updateRecord (Client API reference)

Updates a table record.

Syntax

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

Parameters

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

A JSON object containing key: value pairs, where `key` is the property of the table 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 table 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 values 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 tables using the Web API to define the data object for updating a table 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 table 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
    }
);

To update association to the related tables of type Activity, set the value of single-valued navigation properties using the @odata.bind annotation to another record.

Update related opportunity column on task

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

Update Regarding column on task

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

Update associations for collection-valued navigation properties

The Xrm.WebApi.online.execute API can be used to associate and disassociate collection-valued navigation properties. This is NOT supported for mobile offline scenarios.

Xrm.WebApi