如何在 Dynamics CRM 中显示帐户和子帐户的联系人

How to display contacts for account and sub-account in Dynamics CRM

我分享这个是因为我花了很长时间才找到一种显示父帐户及其所有子帐户的所有联系人的好方法。因此,在查看子帐户时,它将仅显示该帐户的联系人,但对于主帐户,它将显示该帐户的联系人及其子帐户的联系人。

首先在帐户表单编辑器中为联系人插入一个子网格,为其命名,给它一个标签来显示。有如下图的选项。

然后将下面的代码添加为网络资源(JavaScript)

function GetContacts() {

// get Contacts Sub grid
var accountChildContactsGrid = window.parent.document.getElementById("whatever your sub grid name is")


// Get the ID of the current account
var rootAccountID = Xrm.Page.data.entity.getId();

// Check that the subgrid is ready
if (accountChildContactsGrid == null){
    setTimeout('GetContacts()',1000);
    return;
}

// Construct FetchXML for contacts in this account and its child accounts
var fetchXml = "<fetch version='1.0' mapping='logical'>";
fetchXml += "<entity name='contact'>";
fetchXml += "<attribute name='fullname'/>";
fetchXml += "<attribute name='emailaddress1'/>";
fetchXml += "<order attribute='fullname' descending='false' />";
fetchXml += "<link-entity name='account' from='accountid' to='parentcustomerid' link-type='inner' >";
fetchXml += "<filter type='and'>";
fetchXml += "<condition attribute='accountid' operator='eq-or-under' value='" + rootAccountID + "' />";
fetchXml += "<condition attribute='accountid' operator='not-null' />";
fetchXml += "</filter>";
fetchXml += "</link-entity>";
fetchXml += "</entity>";
fetchXml += "</fetch>";

// make sure control is ready and set data and refresh the subgrid
if (accountChildContactsGrid.control != null){  
    accountChildContactsGrid.control.SetParameter("fetchXml", fetchXml);
    accountChildContactsGrid.control.refresh();
}
else{
    setTimeout('GetContacts()',1000);
}

}

最后,回到帐户表单编辑器,转到表单属性并将资源添加到库中,并添加一个加载操作,该操作将按照下图调用我们的 GetContacts() 函数

我希望这对某人有所帮助

注意:此解决方案适用于 Dynamics CRM Online 2015 (7.1) 注意 2:您需要已经设置了层次关系功能才能发挥作用