{"id":15362,"date":"2019-04-10T12:01:54","date_gmt":"2019-04-10T12:01:54","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=15362"},"modified":"2021-12-06T14:55:38","modified_gmt":"2021-12-06T09:25:38","slug":"retrieval-of-many-to-many-nn-relationship-records-using-web-api-dynamics-365","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2019\/04\/retrieval-of-many-to-many-nn-relationship-records-using-web-api-dynamics-365\/","title":{"rendered":"Retrieval of Many-to-Many (N:N) Relationship Records using Web API in Dynamics 365"},"content":{"rendered":"<h2><strong>Introduction<\/strong><\/h2>\n<p>In this blog we will learn to retrieve Dynamic CRM entity records in respect of Many to Many relationships between two Dynamic CRM entities (OOB\/Custom).<\/p>\n<p>So let\u2019s take a look at the following example:<\/p>\n<p>In the below screeshot, there is one Many-to-Many relationship present between Account and Territory Entity of Dynamic CRM.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-15363\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/04\/1Web-API-in-Dynamics-365.png\" alt=\"Web API in Dynamics 365\" width=\"1366\" height=\"676\" style=\"border:1px solid #0a0a0a; padding:1px; margin:1px;\"\/><\/p>\n<p>One Territory entity subgrid is added to the Account entity form.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-15364\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/04\/2Web-API-in-Dynamics-365.png\" alt=\"Web API in Dynamics 365\" width=\"1351\" height=\"655\" style=\"border:1px solid #0a0a0a; padding:1px; margin:1px;\"\/><\/p>\n<p>So, now we need to retrieve Account entity records which are associated with the three territories shown in the above screenshot (i.e California, Nevada, New York). For this, we need to first prepare the fetchXml query for retrieving the Account entity records.<\/p>\n<p>&lt;fetch version=&#8221;1.0&#8243; output-format=&#8221;xml-platform&#8221; mapping=&#8221;logical&#8221; distinct=&#8221;true&#8221;&gt;<\/p>\n<p>&lt;entity name=&#8221;account&#8221;&gt;<\/p>\n<p>&lt;attribute name=&#8221;name&#8221; \/&gt;<\/p>\n<p>&lt;order attribute=&#8221;name&#8221; descending=&#8221;false&#8221; \/&gt;<\/p>\n<p>&lt;link-entity name=&#8221;new_account_territory_entity_name&#8221; from=&#8221;accountid&#8221; to=&#8221;accountid&#8221;\u00a0\u00a0\u00a0 visible=&#8221;false&#8221; intersect=&#8221;true&#8221;&gt;<\/p>\n<p>&lt;link-entity name=&#8221;territory&#8221; from=&#8221;territoryid&#8221; to=&#8221;territoryid&#8221; alias=&#8221;ab&#8221;&gt;<\/p>\n<p>&lt;filter type=&#8221;and&#8221;&gt;<\/p>\n<p>&lt;filter type=&#8221;or&#8221;&gt;<\/p>\n<p>&lt;condition attribute=&#8221;name&#8221; operator=&#8221;eq&#8221; value=&#8221;Nevada&#8221; \/&gt;<\/p>\n<p>&lt;condition attribute=&#8221;name&#8221; operator=&#8221;eq&#8221; value=&#8221;New York&#8221; \/&gt;<\/p>\n<p>&lt;\/filter&gt;<\/p>\n<p>&lt;\/filter&gt;<\/p>\n<p>&lt;attribute name=&#8221;name&#8221; \/&gt;<\/p>\n<p>&lt;attribute name=&#8221;territoryid&#8221; \/&gt;<\/p>\n<p>&lt;\/link-entity&gt;<\/p>\n<p>&lt;\/link-entity&gt;<\/p>\n<p>&lt;\/entity&gt;<\/p>\n<p>&lt;\/fetch&gt;<\/p>\n<p>&nbsp;<\/p>\n<blockquote><p><strong>Note:<\/strong><\/p>\n<p><strong>new_account_territory_entity_name: It is defined as IntersectEntityName which you can get from N:N relationship record as shown in the screenshot below.<\/strong><\/p>\n<p><strong>accountid:\u00a0 It is defined as Entity1IntersectAttribute attribute which you can get from N:N relationship record.<\/strong><\/p><\/blockquote>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-15365\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/04\/3Web-API-in-Dynamics-365.png\" alt=\"Web API in Dynamics 365\" width=\"1366\" height=\"675\" style=\"border:1px solid #0a0a0a; padding:1px; margin:1px;\"\/><\/p>\n<p>Now run this fetchXml query using Web API.<\/p>\n<p>\/\/create AJAX request<\/p>\n<p>Maplytics_jQ.ajax({<\/p>\n<p>type: &#8220;GET&#8221;,<\/p>\n<p>contentType: &#8220;application\/json; charset=utf-8&#8221;,<\/p>\n<p>datatype: &#8220;json&#8221;,<\/p>\n<p>\/\/async: false,<\/p>\n<p>url: this.getWebAPIPath() + entitySetName + &#8220;?fetchXml=&#8221; + fetchXml,<\/p>\n<p>beforeSend: function (xhr) {<\/p>\n<p>\/\/Specifying this header ensures that the results will be returned as JSON.<\/p>\n<p>xhr.setRequestHeader(&#8220;Accept&#8221;, &#8220;application\/json&#8221;);<\/p>\n<p>xhr.setRequestHeader(&#8220;Content-Type&#8221;, &#8220;application\/json; odata.metadata=minimal&#8221;);<\/p>\n<p>xhr.setRequestHeader(&#8220;OData-MaxVersion&#8221;, &#8220;4.0&#8221;);<\/p>\n<p>xhr.setRequestHeader(&#8220;OData-Version&#8221;, &#8220;4.0&#8221;);<\/p>\n<p>xhr.setRequestHeader(&#8220;Prefer&#8221;, &#8220;odata.include-annotations=*&#8221;);<\/p>\n<p>},<\/p>\n<p>success: function (data, textStatus, xhr) {<\/p>\n<p>&nbsp;<\/p>\n<p>},<\/p>\n<p>error: function (xhr, textStatus, errorThrown) {<\/p>\n<p>retrieveUsingFetchError(Inogic.Maplytics.ApiLib.errorHandler(xhr));<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>The above query will help to retrieve Account records which are associated with three territories records (i.e Nevada, New York, California).<\/p>\n<h2 style=\"text-align: left;\"><div class=\"su-heading su-heading-style-default su-heading-align-center\" id=\"\" style=\"font-size:15px;margin-bottom:5px\"><div class=\"su-heading-inner\">One Pic = 1000 words! Analyze data 90% faster with visualization apps!<\/div><\/div><\/h2>\n<p style=\"text-align: left;\"><em>Get optimum visualization of Dynamics 365 CRM data with &#8211;<\/em><br \/>\n<em><strong><a href=\"https:\/\/bit.ly\/3lYvozZ\" target=\"_blank\" rel=\"noopener noreferrer\">Kanban Board<\/a> <\/strong>\u2013 Visualize Dynamics 365 CRM data in Kanban view by categorizing entity records in lanes and rows as per their status, priority, etc.<\/em><br \/>\n<em><strong><a href=\"https:\/\/bit.ly\/3lCSBaA\" target=\"_blank\" rel=\"noopener noreferrer\">Map My Relationships<\/a><\/strong> \u2013 Map My Relationships \u2013 Visualize connections and relationships between Dynamics 365 CRM entities or related records in a Mind Map view.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction In this blog we will learn to retrieve Dynamic CRM entity records in respect of Many to Many relationships between two Dynamic CRM entities (OOB\/Custom). So let\u2019s take a look at the following example: In the below screeshot, there is one Many-to-Many relationship present between Account and Territory Entity of Dynamic CRM. One Territory\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2019\/04\/retrieval-of-many-to-many-nn-relationship-records-using-web-api-dynamics-365\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":15366,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[18,19,65],"tags":[1823],"class_list":["post-15362","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dynamics-365-v9-2","category-dynamics-crm","category-webapi","tag-web-api-in-dynamics-365"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/15362","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/comments?post=15362"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/15362\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/15366"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=15362"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=15362"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=15362"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}