在加载子网格表单时获取父属性

Obtain parent attribute on load of sub-grid form

我有一个实体 A 的表单,上面有一个实体 B 的子网格。子网格(实体 B)的表单包含一个选项集,我的要求是根据实体 A 的属性值限制实体 B 表单上可以 select 的值。我如何获得实体 B 表单上的实体 A 属性值使用 JavaScript?

您可以查询OData endpoint。在此示例中,我假设在联系人表单中并检索父帐户的关系类型属性。

var accountId = Xrm.Page.getAttribute('parentaccountid').getValue()[0].id;
var url = Xrm.Page.context.getClientUrl();

// OData url. Note that you have to use Schema Names (it's case-sensitive)
url = url + "/XRMServices/2011/OrganizationData.svc/AccountSet(guid'"+accountId+"')?$select=CustomerTypeCode";

var req = new XMLHttpRequest();
req.open('GET', url, false); // synchronous call, to keep code short
req.setRequestHeader("Accept", "application/json");
req.setRequestHeader("Content-Type", "application/json; charset=utf-8");
req.send(null);
var retrievedData = JSON.parse(req.responseText);

// accountData.CustomerTypeCode.Value now contains the numerical value of the optionset

我使用了SDK.REST.retrieveRecord函数。我们可以从实体 B 上的查找字段(实体 A 与实体 B 有 1:N 关系)中获取父记录 ID(此处为实体 A),并使用上面的 REST 函数检索所有相关记录。