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

时间:2016-01-19 03:21:31

标签: javascript crm microsoft-dynamics dynamics-crm-2015 fetchxml

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

1 个答案:

答案 0 :(得分:1)

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

Subgrid options

然后将以下代码添加为Web资源(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);
}

}

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

enter image description here

我希望这有助于某人

注意:此解决方案适用于Dynamics CRM Online 2015(7.1) 注意2:您需要具有已设置的层次关系功能才能实现此功能