updateRecord (Client API reference)

Applies to Dynamics 365 for Customer Engagement apps version 9.x

Updates an entity record.

Syntax

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

Parameters

Name Type Required Description
entityLogicalName String Yes The entity logical name of the record you want to update. For example: "account".
id String Yes GUID of the entity record you want to update.
data Object Yes

A JSON object containing key: value pairs, where key is the property of the entity and value is the value of the property you want to update.

See examples later in this topic to see how you can define the data object for various update scenarios.

successCallback Function No

A function to call when a record is updated. An object with the following properties will be passed to identify the updated record:

  • entityType: String. The entity type of the updated record.
  • id: String. GUID of the updated record.
errorCallback Function No A function to call when the operation fails. An object with the following properties will be passed:
  • errorCode: Number. The error code.
  • message: String. An error message describing the issue.

Return Value

On success, returns a promise object containing the attributes specified earlier in the description of the successCallback parameter.

Examples

These examples use some of the same request objects as demonstrated in Update and delete entities using the Web API to define the data object for updating an entity record.

Basic update

Updates an existing account record with record ID = 5531d753-95af-e711-a94e-000d3a11e605.

// define the data to update a record
var data =
    {
        "name": "Updated Sample Account ",
        "creditonhold": true,
        "address1_latitude": 47.639583,
        "description": "This is the updated description of the sample account",
        "revenue": 6000000,
        "accountcategorycode": 2
    }
// update the record
Xrm.WebApi.updateRecord("account", "5531d753-95af-e711-a94e-000d3a11e605", data).then(
    function success(result) {
        console.log("Account updated");
        // perform operations on record update
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

To update association to the related entity records (lookups), set the value of single-valued navigation properties using the @odata.bind annotation to another record. However, for mobile clients in the offline mode, you cannot use the @odata.bind annotation, and instead have to pass a lookup object (logicalname and id) pointing to the target record. Here are code examples for both the scenarios:

For online scenario (connected to server)

The following example updates an account record to associate another contact record as the primary contact for the account:

// define the data to update a record
var data =
    {
        "primarycontactid@odata.bind": "/contacts(61a0e5b9-88df-e311-b8e5-6c3be5a8b200)"
    }
// update the record
Xrm.WebApi.updateRecord("account", "5531d753-95af-e711-a94e-000d3a11e605", data).then(
    function success(result) {
        console.log("Account updated");
        // perform operations on record update
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

For mobile offline scenario

Here is the updated sample code to update an account record to associate another contact record as the primary contact for the account from mobile clients when working in the offline mode:

// define the data to update a record
var data =
    {
        "primarycontactid":
        {
            "logicalname": "contact",
            "id": "61a0e5b9-88df-e311-b8e5-6c3be5a8b200"
        }
    }
// update the record
Xrm.WebApi.offline.updateRecord("account", "5531d753-95af-e711-a94e-000d3a11e605", data).then(
    function success(result) {
        console.log("Account updated");
        // perform operations on record update
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

Xrm.WebApi