retrieveRecord (客户端 API 参考)

检索表记录。

Syntax

Xrm.WebApi.retrieveRecord(entityLogicalName, id, options).then(successCallback, errorCallback);

参数

Name 类型 必选 Description
entityLogicalName String 是的 要检索的记录的表逻辑名称。 例如: account
id String 是的 要检索的表记录的 GUID。
options String OData 系统查询选项,用于控制返回的内容。 请参阅 选项
successCallback 功能 检索记录时要调用的函数。 一个 JSON 对象,其中包含已检索的属性和传递给函数的值。
errorCallback 功能 作失败时要调用的函数。 传递具有以下属性的对象:
- errorCode:数。 错误代码为正十进制数。 例如,将按原样0x80040333记录的错误代码作为返回 。2147746611
- message:字符串。 描述问题的错误消息。

选项

若要控制返回的内容,请使用 $select OData $expand 系统查询选项检索数据。

$select使用系统查询选项限制通过包括以逗号分隔的属性名称列表返回的属性。 选择特定属性是一个重要的性能最佳做法。 如果未使用 $select指定属性,则返回所有属性。

$expand使用系统查询选项控制返回相关表中的数据。 如果只是包含导航属性的名称,则会收到相关记录的所有属性。 可以使用导航属性名称后括号中的系统查询选项限制为相关记录 $select 返回的属性。 将此函数用于 单值集合值 导航属性。 对于脱机,我们仅支持嵌套$select选项。$expand

您指定以 ?. 您还可以通过使用 for & 分隔查询选项来指定多个查询选项。 例如:

?$select=name&$expand=primarycontactid($select=contactid,fullname)

请参阅 示例 ,了解如何为各种检索方案定义选项参数。

返回值

成功后,返回一个承诺,其中包含包含包含已检索列及其值的 JSON 对象。 如果请求的记录不存在,则返回错误。

例子

请参阅以下示例:

基本检索

检索记录 ID 为 5531d753-95af-e711-a94e-000d3a11e605 的帐户记录的名称和收入。

Xrm.WebApi.retrieveRecord("account", "a8a19cdd-88df-e311-b8e5-6c3be5a8b200", "?$select=name,revenue").then(
    function success(result) {
        console.log("Retrieved values: Name: " + result.name + ", Revenue: " + result.revenue);
        // perform operations on record retrieval
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

上面的示例在控制台中显示以下文本;可能会看到其他值,具体取决于数据:

Retrieved values: Name: Sample Account, Revenue: 5000000

以下示例演示如何检索记录 ID 为 =a8a19cdd-88df-e311-b8e5-6c3be5a8b200 的帐户记录的联系人。 对于相关联系人记录,我们仅检索 contactidfullname 属性。

Xrm.WebApi.retrieveRecord("account", "a8a19cdd-88df-e311-b8e5-6c3be5a8b200", "?$select=name&$expand=primarycontactid($select=contactid,fullname)").then(
    function success(result) {
        console.log("Retrieved values: Name: " + result.name + ", Primary Contact ID: " + result.primarycontactid.contactid +
                ", Primary Contact Name: " + result.primarycontactid.fullname);
        // perform operations on record retrieval
    },
    function (error) {
        console.log(error.message);
        // handle error conditions
    }
);

上面的示例在控制台中显示以下文本;可能会看到其他值,具体取决于数据:

Retrieved values: Name: Adventure Works, Primary Contact ID: 49a0e5b9-88df-e311-b8e5-6c3be5a8b200, Primary Contact Name: Adrian Dumitrascu

Xrm.WebApi.retrieveMultipleRecords
Xrm.WebApi