{"id":32394,"date":"2022-08-16T12:00:29","date_gmt":"2022-08-16T06:30:29","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=32394"},"modified":"2022-08-16T12:00:29","modified_gmt":"2022-08-16T06:30:29","slug":"modify-and-delete-multi-table-lookups-within-dynamics-365-crm","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2022\/08\/modify-and-delete-multi-table-lookups-within-dynamics-365-crm\/","title":{"rendered":"Modify and delete multi-table lookups within Dynamics 365 CRM"},"content":{"rendered":"<p>In our previous <a href=\"https:\/\/www.inogic.com\/blog\/2021\/08\/multi-entity-table-lookup-in-dynamics-365-crm\/\" target=\"_blank\" rel=\"noopener\">blog<\/a>, we looked at \u201chow to create a lookup field with multiple tables\u201d, where we created the Distributor\u00a0lookup with <strong>Account<\/strong>, <strong>Contact<\/strong> and <strong>Lead<\/strong> tables on the custom table <strong>Retailer<\/strong>. In this blog, we will see how to add or remove tables from an existing multi-table lookup. For which we shall add the <strong>Appointment<\/strong> table and remove the <strong>Contact<\/strong> table from the Distributor\u00a0lookup.<\/p>\n<p><strong>Scenarios:<\/strong><\/p>\n<ul>\n<li><strong>Add a table to a multi-table lookup.<\/strong><\/li>\n<\/ul>\n<p>To add a table to an existing multi-table lookup, we need to define a new 1: N relationship between the table which we want to add to the lookup and the table on which the lookup is placed.<\/p>\n<p>As per our example, we want to add the <strong>Appointment<\/strong> table to the <strong>Distributor<\/strong> lookup on the <strong>Retailer<\/strong> table. So, we will define the 1: N relationship from the <strong>Appointment<\/strong> table to the <strong>Retailer<\/strong> table.<\/p>\n<p>Below is the C# code using which we can define the 1: N relationship programmatically which will add the <strong>Appointment<\/strong> table to the lookup.<\/p>\n<pre class=\"lang:css gutter:true start:1\">public void AddRelationshipToExistingPolymorphic(IOrganizationService service, ITracingService trace)\r\n\r\n{\r\n\r\nvar createRelationshipRequest = new CreateOneToManyRequest();\r\n\r\n\/\/ Define the new relationship details.\r\n\r\nvar oneToManyRelationship = new OneToManyRelationshipMetadata\r\n\r\n{\r\n\r\nReferencingEntity = \"ikl_retailer\",\r\n\r\nReferencedEntity = \"appointment\",\r\n\r\nSchemaName = \"ikl_retailer_appointment\"\r\n\r\n};\r\n\r\ncreateRelationshipRequest.OneToManyRelationship = oneToManyRelationship;\r\n\r\n\/\/ Define the existing lookup details.\r\n\r\ncreateRelationshipRequest.Parameters[\"Lookup\"] = new LookupAttributeMetadata()\r\n\r\n{\r\n\r\nSchemaName = \"ikl_Distributor\",\r\n\r\nDisplayName = new Label(\"Distributor\", 1033)\r\n\r\n\u00a0\r\n\r\n};\r\n\r\nservice.Execute(createRelationshipRequest);\r\n\r\n}<\/pre>\n<p>From the below screenshot we can see the Distributor lookup allows us to select Appointment records as well, after adding the relationship to the same.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-32395\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/1Multi-table-lookups.jpeg\" alt=\"Multi-table lookups\" width=\"1163\" height=\"428\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/1Multi-table-lookups.jpeg 1163w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/1Multi-table-lookups-300x110.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/1Multi-table-lookups-1024x377.jpeg 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/1Multi-table-lookups-768x283.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/1Multi-table-lookups-660x243.jpeg 660w\" sizes=\"(max-width: 1163px) 100vw, 1163px\" \/><\/p>\n<ul>\n<li><strong>Remove a table from the multi-table lookup.<\/strong><\/li>\n<\/ul>\n<p>To remove a table from an existing multi-table lookup, we need to remove the existing 1: N relationship.<\/p>\n<p>As per our example, we want to remove the <strong>Contact<\/strong> table from the <strong>Distributor<\/strong> lookup on the <strong>Retailer<\/strong> table. So, we will remove the 1: N relationship between the <strong>Contact<\/strong> table and the <strong>Retailer<\/strong> table, for which we would need the relationship schema name.<\/p>\n<p>As per our example, the unique relationship schema name for the 1: N relation defined between <strong>Contact<\/strong> and <strong>Retailer<\/strong> is \u201cikl_retailer_contact&#8221;.<\/p>\n<p>Below is the C# code using which we can delete the 1: N relationship programmatically which will remove the <strong>Contact<\/strong> table from the lookup.<\/p>\n<pre class=\"lang:css gutter:true start:1\">public void DeleteRelationship(IOrganizationService service, ITracingService trace)\r\n\r\n{\r\n\r\nvar deleteRelationshipRequest = new DeleteRelationshipRequest\r\n\r\n{\r\n\r\n\/\/ Name of the relationship to be deleted\r\n\r\nName = \"ikl_retailer_contact\"\r\n\r\n};\r\n\r\nservice.Execute(deleteRelationshipRequest);\r\n\r\n}<\/pre>\n<p>From the below screenshot we can see the Contact table is no more available for selection in the Distributor lookup.<\/p>\n<p><img decoding=\"async\" class=\"alignnone size-full wp-image-32396\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/2Multi-table-lookups.jpeg\" alt=\"Multi-table lookups\" width=\"1157\" height=\"460\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/2Multi-table-lookups.jpeg 1157w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/2Multi-table-lookups-300x119.jpeg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/2Multi-table-lookups-1024x407.jpeg 1024w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/2Multi-table-lookups-768x305.jpeg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/2Multi-table-lookups-660x262.jpeg 660w\" sizes=\"(max-width: 1157px) 100vw, 1157px\" \/><\/p>\n<ul>\n<li><strong>Delete the multi-table lookup.<\/strong><\/li>\n<\/ul>\n<p>There could be a scenario where we would want to delete the multi-table lookup itself. For this, we need to write the below C# code that would delete the lookup itself.<\/p>\n<pre class=\"lang:css gutter:true start:1\">public void DeleteAttribute(IOrganizationService service, ITracingService trace)\r\n\r\n{\r\n\r\nDeleteAttributeRequest deleteAttributeRequest = new DeleteAttributeRequest();\r\n\r\n\/\/Table name where the lookup attribute exists\r\n\r\ndeleteAttributeRequest.EntityLogicalName = \"ikl_retailer\";\r\n\r\n\/\/Existing lookup logicalname\r\n\r\ndeleteAttributeRequest.LogicalName = \"ikl_distributor\";\r\n\r\nservice.Execute(deleteAttributeRequest);\r\n\r\n\u00a0\r\n\r\n}<\/pre>\n<h2>Conclusion:<\/h2>\n<p>Thus, we saw how we can further manipulate the multi-table lookup field programmatically.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/product\/productivity-apps\/undo-restore-recover-deleted-dynamics-365-crm-records\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" class=\"alignnone  wp-image-32398\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/c2u.jpg\" alt=\"\" width=\"820\" height=\"205\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/c2u.jpg 800w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/c2u-300x75.jpg 300w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/c2u-768x192.jpg 768w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2022\/08\/c2u-660x165.jpg 660w\" sizes=\"(max-width: 820px) 100vw, 820px\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our previous blog, we looked at \u201chow to create a lookup field with multiple tables\u201d, where we created the Distributor\u00a0lookup with Account, Contact and Lead tables on the custom table Retailer. In this blog, we will see how to add or remove tables from an existing multi-table lookup. For which we shall add the\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2022\/08\/modify-and-delete-multi-table-lookups-within-dynamics-365-crm\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":0,"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,2361],"tags":[2623],"class_list":["post-32394","post","type-post","status-publish","format-standard","hentry","category-dynamics-365-v9-2","category-technical","tag-multi-table-lookups"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/32394","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=32394"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/32394\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=32394"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=32394"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=32394"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}