更新表记录。
Syntax
Xrm.WebApi.updateRecord(entityLogicalName, id, data).then(successCallback, errorCallback);
参数
| Name | 类型 | 必选 | Description |
|---|---|---|---|
entityLogicalName |
String | 是的 | 要更新的记录的表逻辑名称。 例如: account。 |
id |
String | 是的 | 要更新的表记录的 GUID。 |
data |
物体 | 是的 | 包含 key: value 对的 JSON 对象,其中 key 表的属性和 value 要更新的属性的值。请参阅 示例 ,了解如何为各种更新方案定义 data 对象。 |
successCallback |
功能 | 否 | 更新记录时要调用的函数。 请参阅 返回值 |
errorCallback |
功能 | 否 | 作失败时要调用的函数。 传递具有以下属性的对象: - errorCode:数。 错误代码为正十进制数。 例如,将按原样0x80040333记录的错误代码作为返回 。2147746611- message:字符串。 描述问题的错误消息。 |
返回值
成功后,向具有以下属性的 successCallback promise 对象返回:
| Name | 类型 | Description |
|---|---|---|
entityType |
String | 记录的表逻辑名称。 |
id |
String | 记录的 GUID。 |
例子
这些示例使用一些相同的请求对象,如 使用 Web API 更新和删除表行 中所示,以定义用于更新表记录的数据对象。
| 示例 | 演示 | Web API 示例 |
|---|---|---|
| 基本更新 | 如何更新现有帐户记录上的列值。 | 基本更新 |
| 更新与相关表的关联 | 如何使用 < a0/&a0> 更改帐户的主要联系人 |
与单值导航属性关联 |
| 更新与 Activity 类型的相关表的关联 | 如何将任务与机会相关联,或将其“关于查找”设置为帐户。 | 与单值导航属性关联 |
| 更新集合值导航属性的关联 | 如何使用 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
}
);
移动脱机方案的弃用方法
注释
现有自定义项仍支持弃用具有区分大小写属性的查阅对象,logicalnameid而不是在前面的示例中使用@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
}
);
更新与活动类型相关表的关联示例
若要更新与 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 关联和取消关联集合值导航属性。 移动脱机方案不支持此功能。