更新表记录。
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 更新和删除表行 中所示,以定义用于更新表记录的数据对象。
基本更新
使用记录 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
}
);
移动脱机方案的弃用方法
注释
除了使用 @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 类型的相关表的关联
若要更新与 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 可用于关联和取消关联集合值导航属性。 移动脱机方案 不支持 此作。