Update Record Using XRM WebAPI With Webresource In Dynamics CRM

Introduction

In Dynamics 365 CRM, for certain requirements data needs to be updated for a given record, based on GUID of an entity record. As an example, for a selected contact record respective fields details were passed to update contact record.

Step 1

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

Update Record 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 Record using XRM WebApi with Webresource in Dynamics CRM

Step 3

After Step 2, we can use Xrm.WebApi with update Record which expects required string type fields entity logical name, id and data of object type with the following syntax as

Xrm.WebApi.updateRecord(entityLogicalName, id, data).then(successCallback, errorCallback);

As,

var data = {
    "emailaddress1": "test@gmail.com",
    "telephone1": "1234456",
    "mobilephone": "7834567890",
    "address1_line1": "Hyderabad",
    "address1_country": "India"
}
// update the record
Xrm.WebApi.updateRecord("contact", contactGUID, data).then(function success(result) {
    console.log("Contact updated");
    // perform operations on record update
}, function(error) {
    console.log(error.message);
    // handle error conditions
});

Step 4

After Step 3, summing it together the final code will look like below which needs to be called from respective on load function

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 = {
            "emailaddress1": "test@gmail.com",
            "telephone1": "1234456",
            "mobilephone": "7834567890",
            "address1_line1": "Hyderabad",
            "address1_country": "India"
        }
        // update the record
        Xrm.WebApi.updateRecord("contact", contactGUID, data).then(function success(result) {
            console.log("Contact updated");
            // perform operations on record update
        }, function(error) {
            console.log(error.message);
            // handle error conditions
        });
    }
}

Step 5

After Step 4, save and publish Webresource which was registered on onload function of Contact Form and open contact record whose GUID was passed in the update request and observe the console window to see the message Contact Updated and refresh page to see the details to get reflect on the form as shown in the below figure.

Update Record using XRM WebApi with Webresource in Dynamics CRM

Note

  1. Make sure to publish all customizations and upload JavaScript (js) file.
  2. As an example, record guid was hardcoded, but this can be dynamically generated based on business requirements.
  3. I have mainly concentrated on the process to build object with contact fields that needs to be updated in the given record.
  4. Make sure to provide logical names of contact entity fields, otherwise errors will get encountered during on load of the record.
  5. Microsoft documentation can be found here

Conclusion

In this way, one can easily perform basic update using XRM Web API updateRecord method using web resource.