Enable Javascript

Please enable Javascript to view website properly

Toll Free 1800 889 7020

Looking for an Expert Development Team? Take 2 weeks Free Trial! Try Now

Get all Contacts of All Hierarchical Accounts Using FetchXML and Web API

To demonstrate, I’ve created an HTML web resource with 1 button on the contact form. When clicking on the button, I will ask the contacts to get a list of all the contacts under the account hierarchy.

webapi

After clicking on the Parent Account button, we will get a list of all Contacts under the Account hierarchy. Please check the below Image. It will be the output of our development. Now to get this output, what we need to do in the HTML file, is that I am going to explain.

webapi

Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM Services Online 2015 Update introduce the capability to define specific self-referencing one-to-many entity relationships as hierarchical. You can write queries that return related data in these hierarchies.

With Web API in MS CRM, we retrieve data using FetchXML. An easy way to construct FetchXML queries is using the advanced find in CRM, you can create queries in advanced find, add columns, define sorting, and then download FetchXML.

Created an Account hierarchy like this:

webapi

Once your FetchXML is ready, you can use this FetchXML in web API to retrieve data from CRM. Here is a sample code for how to execute FetXML and get values from the FetchXML result set. Please note that we need to pass the Top most Account ID of the hierarchy (Main/Root Account). If you can’t have that Root Account ID with you, I will explain to you how to get that using any Account Id you have later in this blog as well.

Now define the FetchXML and web API as below:

//Get the All Contacts of Root and Child Accounts //we will get using the standard Xrm.Page.context.getClientUrl() function var apiQuery2 = Xrm.Page.context.getClientUrl() + "contacts"; //Construct FetchXML for contacts in this parent account var fetchXML2 = " <fetch version='1.0' mapping='logical' distinct='true'> " + " <entity name='contact'> " + " <attribute name='contactid' />" + " <attribute name='fullname' />" + " <attribute name='emailaddress1' />" + " <order attribute='fullname' descending='false' />" + " <link-entity name='account' from='accountid' to='parentcustomerid' link-type='inner'> " + " <filter type='and'> " + " <condition attribute='accountid' operator='eq-or-under' value=' {" + Your Parent Account ID + "}' />" + " <condition attribute='accountid' operator='not-null' />" + " </filter>" + " </link-entity>" + " </entity>" + " </fetch>"; //With our request path string built, we can perform a standard XmlHttpRequest to retrieve the data from the web service and work with the results however we wish. var encodedFetchXML2 = encodeURIComponent(fetchXML2); var req = new XMLHttpRequest(); req.open("GET", apiQuery2 + "?fetchXml=" + encodedFetchXML2, false); req.setRequestHeader("OData-MaxVersion", "4.0"); req.setRequestHeader("OData-Version", "4.0"); req.setRequestHeader("Accept", "application/json"); req.onreadystatechange = function () { if (this.readyState === 4) { req.onreadystatechange = null; if (this.status === 200) { var results = JSON.parse(this.responseText); // get all contacts of the parent account var _finalContacts = results.value; for (var i = 0; i < _finalContacts.length; i++) { str +='<tr><td>' + _finalContacts[i].fullname + '</td></tr>' ; } $( '#myTableData' ).append(str); } } }; req.send();

So the execution of the above code will show you the output as I showed in the above image. It will list down all Contacts we have about all Accounts of that hierarchy.

If you want to test your fetch XML before development. You can use the “FetchXML Tester” tool from XrmToolBox, you can put your query there and click on Execute.

webapi

Now, you click on the Response button and it will show something like this to get all contact records of all accounts in the hierarchy of the Main Account you passed.

webapi

How to get a root account using FetchXML using any of those hierarchical accounts?

Now many of you will have a question, how to get a root account ID if you have an account hierarchy. It’s easy to get with FetchXML and Web API. If you have an account ID for that hierarchy, use it in the FetchXML query below.

// FetchXML for Get the Main (Root) Parent Account of the current Account in Nth Level <fetch distinct="false" no-lock="false" mapping="logical"> <entity name="account"> <attribute name="name" /> <attribute name="accountid" /> <attribute name="accountid" rowaggregate="CountChildren" alias="AccountChildren" /> <filter type="and"> <condition attribute="accountid" operator="eq-or-above" value="{Your Current Account ID}" /> <condition attribute="parentaccountid" operator="null" /> </filter> </entity> </fetch>

You will get the below output after executing this query. You can even integrate this into our code in the same way as I did for the contacts above.

webapi
Software Development Team
Need Software Development Team?
captcha
🙌

Thank you!
We will contact soon.

Oops! Something went wrong.

Recent Blogs

Categories

NSS Note
Trusted by Global Clients