Saturday, November 9, 2019

Xrm.WebApi.updateRecord



updateRecord (Client API reference)

Syntax

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

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.
JavaScript
// 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:
JavaScript
// 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 offine 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:
JavaScript
// 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
    }
);

No comments:

Post a Comment