Dynamics 365 – Update Records with JavaScript

Xrm.WebApi.updateRecord() is a method that allows you to update an existing record in Dynamics 365 using the JavaScript and the Web API. In this blog post, I will explain how to update records using JavaScript in CRM, what parameters it accepts, and how to handle the success and error callbacks. I will also show you some use cases and code examples for updating different types of fields, such as lookup, option set, and multi-select option set.

The updateRecord method has the following syntax:

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

The parameters are:

entityLogicalName: A string that represents the logical name of the entity that you want to update. For example, “account” or “contact“.

id: A string that represents the GUID of the record that you want to update. For example, “a3b2c1d4-e5f6-7890-a1b2-c3d4e5f6a7b8”.

data: An object that contains the fields and values that you want to update. For example, {name: "Contoso Ltd", address1_city: "Seattle"}.

successCallback: A function that is executed when the update operation is successful. The function receives a parameter that contains the URI of the updated record. For example, function (result) {console.log("Record updated: " + result.uri);}.

errorCallback: A function that is executed when the update operation fails. The function receives a parameter that contains an object with error details. For example, function (error) {console.log("Error: " + error.message);}.

One of the use cases for updating records using the Web API is to change the owner of a record. For example, if you want to assign an account to a different user, you can use the following code:

var entityLogicalName = "account";
var id = "a3b2c1d4-e5f6-7890-a1b2-c3d4e5f6a7b8"; // GUID of the account record
var data = {
    ownerid: {
        id: "b4c3d2e1-f6e5-8907-b1a2-d3e4f5e6b8a9", // GUID of the new owner
        entityType: "systemuser" // Logical name of the owner entity
    }
};
Xrm.WebApi.updateRecord(entityLogicalName, id, data).then(
    function (result) {
        console.log("Record updated: " + result.uri);
    },
    function (error) {
        console.log("Error: " + error.message);
    }
);

Another use case is to update an option set field. For example, if you want to change the industry of an account, you can use the following code:

var entityLogicalName = "account";
var id = "a3b2c1d4-e5f6-7890-a1b2-c3d4e5f6a7b8"; // GUID of the account record
var data = {
    industrycode: 1 // Value of the option set
};
Xrm.WebApi.updateRecord(entityLogicalName, id, data).then(
    function (result) {
        console.log("Record updated: " + result.uri);
    },
    function (error) {
        console.log("Error: " + error.message);
    }
);

A third use case is to update a multi-select option set field. For example, if you want to add or remove some values from the preferred contact method of a contact, you can use the following code:

var entityLogicalName = "contact";
var id = "c5d4e3f2-g6h7-8901-c2d3-e4f5g6h7i8j9"; // GUID of the contact record
var data = {
    preferredcontactmethodcode: [1, 3] // Array of values to add or remove
};
Xrm.WebApi.updateRecord(entityLogicalName, id, data).then(
    function (result) {
        console.log("Record updated: " + result.uri);
    },
    function (error) {
        console.log("Error: " + error.message);
    }
);

I hope this blog post has helped you understand how to use the Xrm.WebApi.updateRecord method and how to update different types of fields using the Web API. If you have any questions or feedback, please leave a comment below.

Conclusion

To sum up, we’ve covered the Xrm.WebApi.updateRecord() method and how it can be used to update various types of fields in Dynamics 365. We discussed its syntax, handling success and error callbacks, and provided practical examples. We hope this has enhanced your understanding of using the Web API for record updates in Dynamics 365. If you have any questions, feel free to ask!

For more in-depth insights and tutorials on Programming or Dynamics 365 features and customization, check out our posts to further enhance your proficiency and maximize the potential of your skills.

Leave a comment