updateRecord (用戶端 API 參考)

更新表格記錄。

語法

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

參數

名稱 類型 為必填項目 Description
entityLogicalName 繩子 Yes 您要更新之記錄的資料表邏輯名稱。 例如: account 。
id 繩子 Yes 您要更新之資料表記錄的 GUID。
data 物體 Yes 包含 key: value 配對的 JSON 物件,其中 key 是資料表的屬性,是 value 您要更新的屬性值。
請參閱 範例, 以瞭解如何為各種更新案例定義 data 物件。
successCallback 功能 否 更新記錄時要呼叫的函式。 請參閱 傳回值
errorCallback 功能 否 作業失敗時要呼叫的函式。 會傳遞具有下列屬性的物件:
- errorCode:數。 錯誤碼為正十進位數。 例如,記載為 0x80040333 的錯誤碼將傳回為 2147746611。
- message:繩子。 描述問題的錯誤訊息。

返回值

成功時,會傳回具有下列屬性的 Promise successCallback 物件:

名稱 類型 Description
entityType 繩子 記錄的資料表邏輯名稱。
id 繩子 記錄的 GUID。

範例

這些範例會使用一些相同的要求物件,如 使用 Web API 更新和刪除表格列 中所示,來定義更新表格記錄的資料物件。

Example 演示 網頁 API 範例
基本更新 如何更新現有帳戶記錄的欄位值。 基本更新
更新關聯到相關資料表 如何使用 更改帳戶的主要聯絡人 @odata.bind。 與單一價值的導航屬性關聯
將關聯更新到相關類型的活動表 如何將任務與機會關聯,或將其查詢設定到帳號上。 與單一價值的導航屬性關聯
更新集合值導覽屬性的關聯 如何使用 Xrm.WebApi.online.execute。 將紀錄加入收藏 並 從收藏中移除記錄

基本更新範例

更新記錄 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
    }
);

要更新與相關資料表記錄的關聯(查詢),請利用 @odata.bind 註解指向另一筆紀錄,設定單一值導覽屬性的值。

以下是程式碼範例:

下列範例會更新客戶記錄,以將另一個連絡人記錄建立關聯為帳戶的主要連絡人:

// 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
    }
);

Mobile Offline 案例已取代的方法

備註

與@odata.bind前述範例中的註解不同,已棄用且具有大小寫區分特性的logicalname查詢物件(和id)仍支援現有自訂。 不過,建議在線上與離線情境下使用註 @odata.bind 解,而非使用這個已棄用的物件。

下列範例使用已棄用的方法來更新帳戶記錄,以在離線模式下工作時,將另一個連絡人記錄關聯為行動用戶端帳戶的主要連絡人:

// 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
    }
);

要更新與相關 Activity 型式資料表的關聯,請利用 @odata.bind 註解指向另一筆紀錄來設定單一值導航屬性的值。

更新任務上的相關機會欄

// 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
    }
);

更新任務上的相關欄

// 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
   }
);

集合值導覽屬性的更新關聯範例

使用 Xrm.WebApi.online.execute API 來關聯與解除集合值的導航屬性。 此功能不支援行動端離線場景。

Xrm.WebApi