{"id":13399,"date":"2018-11-16T16:36:38","date_gmt":"2018-11-16T11:06:38","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=13399"},"modified":"2021-05-26T13:34:35","modified_gmt":"2021-05-26T13:34:35","slug":"filter-partylist-field-based-on-other-lookup-field-that-supports-on-uci-dynamics-365-v9-x","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2018\/11\/filter-partylist-field-based-on-other-lookup-field-that-supports-on-uci-dynamics-365-v9-x\/","title":{"rendered":"Filter PartyList Field based on other lookup field that supports on UCI Dynamics 365 v9.x"},"content":{"rendered":"<h2><strong>Introduction:<\/strong><\/h2>\n<p>Recently we had a business requirement where client wants to filter PartyList field by selected account in regarding field on activity entity form.<\/p>\n<p>As shown in below image, when we click on party list field, by default it shows data of many entities (i.e. Account, Contact, and User etc.)<\/p>\n<p><img decoding=\"async\" class=\"aligncenter wp-image-13401 \" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2018\/11\/1Filter-PartyList-Field-based-on-other-lookup-field-that-supports-on-UCI-Dynamics-365.png\" alt=\"Filter PartyList Field based on other lookup field that supports on UCI Dynamics 365\" width=\"818\" height=\"627\" \/><\/p>\n<p>Our client requirement is to filter PartyList field \u201cRequired Attendees\u201d and show only Contact based on Account selected in \u201cRegarding\u201d field and that should be supported in UCI.<\/p>\n<p>To achieve this we have developed JavaScript, which apply <strong>addPreSearch <\/strong>to PartyList field \u201cRequired Attendees\u201d.<\/p>\n<p>Here we used \u201c<strong>addCustomFilter\u201d <\/strong>function to apply custom condition and \u201c<strong>setEntityTypes<\/strong>\u201d function to set entity of contact type.<\/p>\n<p><strong>For reference please find JavaScript code as below:<\/strong><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">function filterRequiredAttendees(executionContext) {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0<\/span><span lang=\"EN-US\">\u00a0var functionName = &#8220;filterRequiredAttendees: &#8220;;<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0try {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ get formContext<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var formContext = executionContext.getFormContext();<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Get requiredattendees<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var regardingObject = formContext.getAttribute(&#8220;regardingobjectid&#8221;);<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Validate regarding field<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (!isValid(regardingObject))\u00a0 {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return;<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Validate value of regardingObject<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (regardingObject.getValue() == null) {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return;<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Get requiredattendees control<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var requiredAttendeesControl = formContext.getControl(&#8220;requiredattendees&#8221;);<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Validate requiredattendees field<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (isValid(requiredAttendeesControl)) {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Add PreSearch<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 requiredAttendeesControl.addPreSearch(filterContactByAccount);<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Check if multiple type dropdowns enabled for this lookup<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (requiredAttendeesControl.getEntityTypes().length &gt;= 1) {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Set entity type to contact<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 requiredAttendeesControl.setEntityTypes([&#8216;contact&#8217;]);<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0 }<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0 catch (e) {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Xrm.Navigation.openAlertDialog({ confirmButtonLabel: &#8220;Ok&#8221;, text: e.message, title: functionName });<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0 }<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">}<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">function filterContactByAccount(executionContext) {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0 var functionName = &#8220;filterContactByAccount: &#8220;;<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0 try {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ get formContext<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var formContext = executionContext.getFormContext();<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var regardingObjectValue = formContext.getAttribute(&#8220;regardingobjectid&#8221;).getValue();<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/ Validate Regarding has a value<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 if (regardingObjectValue != null) {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \/\/ Get Account GUID<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var regardingID = regardingObjectValue[0].id;<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 var filterCondition = &#8220;&lt;filter type=&#8217;and&#8217;&gt;&#8221; + &#8220;&lt;condition attribute=&#8217;parentcustomerid&#8217; operator=&#8217;eq&#8217; value='&#8221; + regardingID + &#8220;&#8216; \/&gt;&#8221; + &#8220;&lt;\/filter&gt;&#8221;;<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 formContext.getControl(&#8220;requiredattendees&#8221;).addCustomFilter(filterCondition, &#8220;contact&#8221;);<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0 }<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0 catch (e) {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0Xrm.Navigation.openAlertDialog({ confirmButtonLabel: &#8220;Ok&#8221;, text: e.message, title: functionName });<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0 }<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">}<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">function isValid(attributes) {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0 var functionName = &#8220;isValid &gt;&gt;&#8221;;<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0 try {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0if (attributes != null &amp;&amp; attributes != undefined &amp;&amp; attributes != &#8220;&#8221; &amp;&amp; attributes != &#8220;undefined&#8221; &amp;&amp; attributes != &#8220;null&#8221;) {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return true;<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 else {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 return false;<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 }<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0 } catch (e) {<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 Xrm.Navigation.openAlertDialog({ confirmButtonLabel: &#8220;Ok&#8221;, text: e.message, title: functionName });<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">\u00a0\u00a0\u00a0 }<\/span><\/p>\n<p class=\"x_MsoNormal\"><span lang=\"EN-US\">}<\/span><\/p>\n<p style=\"text-align: justify;\">Add this scripts as web resources, add these libraries to the activity entity form, then we have to set the Form Properties, set \u201cfilterRequiredAttendees\u201d function \u201cOnLoad\u201d of the form, and \u201cOnChange\u201d of the \u201cRegarding\u201d field.<\/p>\n<p style=\"text-align: justify;\">Deploy and publish the changes. Refresh (Ctr+F5) the activity entity form and we are good to filter partylist field.<\/p>\n<p style=\"text-align: justify;\">Now PartyList field \u201cRequired Attendees\u201d, shows only Contact based on Account selected in \u201cRegarding\u201d field.<\/p>\n<p style=\"text-align: justify;\"><img decoding=\"async\" class=\"aligncenter wp-image-13402 \" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2018\/11\/2Filter-PartyList-Field-based-on-other-lookup-field-that-supports-on-UCI-Dynamics-365.png\" alt=\"Filter PartyList Field based on other lookup field that supports on UCI Dynamics 365\" width=\"821\" height=\"641\" \/><\/p>\n<h2><strong>Conclusion<\/strong>:<\/h2>\n<p>Using the \u201c<strong>addPreSearch\u201d, <\/strong>\u201c<strong>addCustomFilter\u201d, <\/strong>and \u201c<strong>setEntityTypes<\/strong>\u201d functions we can filter PartyList fields that supports on UCI too.<\/p>\n<p>Dynamics 365 for Customer Engagement apps version 9.x provided the following client APIs, which are not available in the previous versions.<\/p>\n<p><strong>1. getEntityTypes<\/strong> &#8211; Gets the types of entities allowed in the lookup control<\/p>\n<p><strong>Syntax:<\/strong> formContext.getControl(arg).getEntityTypes();<\/p>\n<p><strong>2. setEntityTypes<\/strong> &#8211; Sets the types of entities allowed in the lookup control.<\/p>\n<p><strong>Syntax:<\/strong> formContext.getControl(arg).setEntityTypes([entityLogicalNames]);<\/p>\n<p><a href=\"https:\/\/www.maplytics.com\/miscellaneous\/effective-sales-territory-management-using-map-visualization-dynamics-crm\/\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter wp-image-13404 \" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2018\/11\/Effectively-Manage-Sales-Territories-on-a-map-within-Microsoft-Dynamics-CRM.png\" alt=\"Effectively-Manage-Sales-Territories-on-a-map-within-Microsoft-Dynamics-CRM\" width=\"820\" height=\"205\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction: Recently we had a business requirement where client wants to filter PartyList field by selected account in regarding field on activity entity form. As shown in below image, when we click on party list field, by default it shows data of many entities (i.e. Account, Contact, and User etc.) Our client requirement is to\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2018\/11\/filter-partylist-field-based-on-other-lookup-field-that-supports-on-uci-dynamics-365-v9-x\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":13400,"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":[16,18,19,27],"tags":[1740,1742],"class_list":["post-13399","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-dynamics-365","category-dynamics-365-v9-2","category-dynamics-crm","category-field-service-2","tag-uci-dynamics-365","tag-uci-dynamics-365-v9-x"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/13399","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=13399"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/13399\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/13400"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=13399"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=13399"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=13399"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}