Update Associations using XRM WebApi With WebResource In Dynamics CRM

Introduction

In Dynamics 365 CRM, for certain requirements we need to update associations in the related tables i.e., update lookup values. As an example, for a selected contact record account lookup will be updated.

Step 1

Login to the required environment and select required solution [Contact Customizations Solution in this case] as shown in the below figure.

Update Associations using XRM WebApi with Webresource in Dynamics CRM

Step 2

After Step 1, select contact web resource in solution and click on Edit as shown in the below figure.

Update Associations using XRM WebApi with Webresource in Dynamics CRM Figure 1

Step 3

After Step 2, we must prepare data for the account lookup in contact record with the below code

var data = {
    "[email protected]": "/accounts(c57ae37c-6cc8-ec11-a7b5-6045bd00d54a)"
}

As shown in the below figure,

Update Associations using XRM WebApi with Webresource in Dynamics CRM
Figure 3

Step 4

After Step 3, prepare request to update record in the related table contact by passing data to updateRecord method of Xrm.WebApi after passing other arguments “contact” and guid of contact like below code

Xrm.WebApi.updateRecord("contact", contactGUID, data).then(function success(result) {
    console.log("Contact updated with Account");
    // perform operations on record update
}, function(error) {
    console.log(error.message);
    // handle error conditions
});

As shown in the below figure

Update Associations using XRM WebApi with Webresource in Dynamics CRM

Step 5

After Step 4, final code looks like below

if (typeof(ContosoVaccination) == "undefined") {
    var ContosoVaccination = {
        __namespace: true
    };
}
if (typeof(ContosoVaccination.Scripts) == "undefined") {
    ContosoVaccination.Scripts = {
        __namespace: true
    };
}
ContosoVaccination.Scripts.ContactForm = {
    handleOnLoad: function(executionContext) {
        console.log('on load - contact form');
        updateContact(executionContext);
    },
    __namespace: true
}

function updateContact(executionContext) {
    let formContext = executionContext.getFormContext();
    if (formContext !== null && formContext != 'undefined') {
        let contactGUID = Xrm.Page.data.entity.getId().replace("{", "").replace("}", "");
        // define the data to update a record
        var data = {
            "[email protected]": "/accounts(c57ae37c-6cc8-ec11-a7b5-6045bd00d54a)"
        }
        // update the record
        Xrm.WebApi.updateRecord("contact", contactGUID, data).then(function success(result) {
            console.log("Contact updated with Account");
            // perform operations on record update
        }, function(error) {
            console.log(error.message);
            // handle error conditions
        });
    }
}

As shown in the below figure

Update Associations using XRM WebApi with Webresource in Dynamics CRM

Step 6

After Step 5, save Webresource and publish it and register this Webresource in form load event of contact form and publish all the customizations and open a contact record which doesn’t have any account, once contact records open one can observe account lookup will be filled with the given sample account guid as shown in the below figure.

Update Associations using XRM WebApi with Webresource in Dynamics CRM

Note

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. To get entity definitions, browse your crm org followed by Entity definition
    1. Ex: https://org12a123ad.api.crm.dynamics.com/api/data/v9.2/$metadata#EntityDefinitions
  3. Make sure to provide exact logical name of account lookup in contact record by looking at navigation property of type mscrm.account in contact table.
  4. Microsoft documentation can be found here

Conclusion

In this way, one can easily perform update associations on the related tables using XRM Web API updateRecord method.


Similar Articles