Back to Basics # 53: 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.  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.

Step 2:

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

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

                }

                );

As shown in the below figure

Step 4:

After Step 3,  summing it together the final code will looks 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

                }

                );

    }

} As shown in the figure

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.

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 requirement.
  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.

One thought on “Back to Basics # 53: Update Record using XRM WebApi with Webresource in Dynamics CRM

  1. Pingback: Back To Basics :Curated List of Articles in a Single Page – Common Man Tips for Power Platform, Dynamics CRM,Azure

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s