更新表格記錄。
語法
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 更新和刪除表格列 中所示,來定義更新表格記錄的資料物件。
基本更新
更新記錄 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 案例已取代的方法
備註
現有的自訂仍支援具有區分大小寫屬性 (logicalname 和 id) 的已棄用查閱物件,而不是使用@odata.bind上述註釋範例。 不過,建議針對線上和離線案例使用 @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
}
);
更新類型為「活動」之相關表格的關聯
若要更新與類型為「活動」之相關資料表的關聯,請使用註釋將 @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 可用來關聯和取消關聯集合值導覽屬性。 Mobile Offline 案例 不支援此功能 。