{"id":20637,"date":"2019-10-01T11:41:10","date_gmt":"2019-10-01T11:41:10","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=20637"},"modified":"2022-07-21T12:08:10","modified_gmt":"2022-07-21T06:38:10","slug":"tip-to-filter-customer-field-depending-on-type-of-customer-in-dynamics-365-uci","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2019\/10\/tip-to-filter-customer-field-depending-on-type-of-customer-in-dynamics-365-uci\/","title":{"rendered":"Tip to Filter Customer Field depending on \u2018Type of Customer\u2019 in Dynamics 365 UCI"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>Recently, we faced an issue in UCI where we had to filter customer field depending on Type of customer.<\/p>\n<p>Here, the requirement was as follows:<\/p>\n<p>If the type of customer is account then the customer field should show Account entity records only.<\/p>\n<p>If the type of customer is contact then the customer field should show Contact entity records only.<\/p>\n<p>For this, we had an option set field named \u2018Type of customer\u2019 which contains \u2018contact\u2019 and \u2018account\u2019 option.<\/p>\n<p>We had added a JavaScript function to set entity type according to value of \u2018Type of customer\u2019 field using setEntityTypes function as shown in following code:<\/p>\n<pre class=\"lang:default decode:true \"> function onLoad(executionContext) {\r\n    try {\r\nvar formContext= executionContext.getFormContext();\r\nformContext.getControl(\"customerid\").addPreSearch(filterCustomerLookup);\r\n    } catch (e) { }\r\n}\r\n\r\nfunction filterCustomerLookup(executionContext) {\r\n    try {\r\n        \tvar formContext= executionContext.getFormContext();\r\n        if (formContext.getAttribute(\"new_customertype\").getValue() == 100000001) {\r\n            formContext.getControl(\"customerid\").setEntityTypes([\"account\"]);\r\n\t} else {\r\n            formContext.getControl(\"customerid\").setEntityTypes([\"contact\"]);\r\n         }\r\n    } catch (e) { }\r\n}\r\n<\/pre>\n<p style=\"text-align: justify;\">This function was working in Classic web UI, but on UCI, the customer was not being filtered correctly.<br \/>\nFor example, in our case, \u2018contact\u2019 was default value of \u2018Type of customer\u2019 and when we were changing \u2018Type of customer\u2019 field value from Contact to Account and clicking on search button of Customer field we were getting Contact records instead of Account records.<\/p>\n<p>Shown in below screenshot is the Records in customer lookup on selecting customer Type Contact:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-20638\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/1Customer-Field.png\" alt=\"Customer Field\" width=\"559\" height=\"582\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/1Customer-Field.png 559w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/1Customer-Field-288x300.png 288w\" sizes=\"(max-width: 559px) 100vw, 559px\" \/><\/p>\n<p>Shown in below screenshot is the Records in customer lookup on selecting customer Type Account:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-20639\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/2Customer-Field.png\" alt=\"Customer Field\" width=\"596\" height=\"556\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/2Customer-Field.png 596w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/2Customer-Field-300x280.png 300w\" sizes=\"(max-width: 596px) 100vw, 596px\" \/><\/p>\n<p>So, we removed the pre-search and tried using below function on Load and on Change of \u2018Type of Customer\u2019 field.<\/p>\n<pre class=\"lang:default decode:true \">function onChangeOfTypeOfCustomer(executionContext) {\r\n    try {\r\nvar formContext= executionContext.getFormContext();\r\n        if (formContext.getAttribute(\"new_typeofcustomer\").getValue() == 100000001) {\/\/100000001 value if Account option in Type of customer field\r\n            formContext.getControl(\"customerid\").setEntityTypes([\"account\"]);\r\n        } else {\r\n            formContext.getControl(\"customerid\").setEntityTypes([\"contact\"]);\r\n        }\r\n    } catch (e) {   }\r\n}<\/pre>\n<p>Again these changes were working in Classic web UI but on UCI, customer was being filtered only for the default entity type.<br \/>\nFor example, in our case contact was default value of \u2018Type of customer\u2019 and when we clicked on new \u2018Type of customer\u2019 field value was being set to Contact by default. As a result, only contact entity was set in Customer field entity types using following code:<br \/>\nformContext.getControl(&#8220;customerid&#8221;).setEntityTypes([&#8220;contact&#8221;]);<br \/>\nWe were getting records of contacts as shown below:<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-20640\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/3Customer-Field.png\" alt=\"Customer Field\" width=\"516\" height=\"559\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/3Customer-Field.png 516w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/3Customer-Field-277x300.png 277w\" sizes=\"(max-width: 516px) 100vw, 516px\" \/><br \/>\nBut, when we were changing \u2018Type of customer\u2019 field value to \u2018Account\u2019, it was not showing any account record and keeps showing \u2018Loading\u2019 text forever, even though we set account entity in Customer field entity types using following code.<\/p>\n<p>formContext.getControl(&#8220;customerid&#8221;).setEntityTypes([&#8220;account&#8221;]);<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-20641\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/4Customer-Field.png\" alt=\"Customer Field\" width=\"543\" height=\"436\" srcset=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/4Customer-Field.png 543w, https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/10\/4Customer-Field-300x241.png 300w\" sizes=\"(max-width: 543px) 100vw, 543px\" \/><\/p>\n<p>On further checking, we observed that this was due to function on \u2018onLoad\u2019 event was directly setting entity type.<\/p>\n<p>So for handling customer type on load event, we had to combine the first original code with changed code.<\/p>\n<p>The final working solution is as below:<\/p>\n<p><strong>Function on Change event of Customer Type field<\/strong>:<\/p>\n<pre class=\"lang:default decode:true \">function onChangeOfCustomerType(executionContext) {\r\ntry {\r\n\r\nvar formContext= executionContext.getFormContext();\r\n\r\nif (formContext.getAttribute(\"new_customertype\").getValue() == 100000001) {\r\nformContext.getControl(\"customerid\").setEntityTypes([\"account\"]);\r\n} else {\r\nformContext.getControl(\"customerid\").setEntityTypes([\"contact\"]);\r\n}\r\n\r\n} catch (e) {\r\n\r\n}\r\n\r\n}<\/pre>\n<p><strong>Function on \u201conLoad\u201d event of form:<\/strong><\/p>\n<pre class=\"lang:default decode:true \"> function setCustomerTypeOnLoad(executionContext) {\r\n    try {\r\n        var formContext= executionContext.getFormContext();\r\n        formContext.getControl(\"customerid\").addPreSearch(filterCustomerLookup);\r\n    } catch (e) {    }\r\n}\r\nfunction filterCustomerLookup(executionContext) {\r\ntry {\r\nvar formContext= executionContext.getFormContext();\r\nif (formContext.getAttribute(\"new_customertype\").getValue() == 100000001) {\r\nformContext.getControl(\"customerid\").setEntityTypes([\"account\"]);\r\n} else {\r\nformContext.getControl(\"customerid\").setEntityTypes([\"contact\"]);\r\n}\r\n} catch (e) { }\r\n}<\/pre>\n<h2>Conclusion<\/h2>\n<p>Thus, with the above code you can easily filter customer field in Dynamics 365 UCI depending upon Type of Customer.<\/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\">70% of global 2000 companies apply gamification to improve productivity and returns!<\/div><\/div><\/h2>\n<p><em><strong><a href=\"https:\/\/bit.ly\/3RD4lYW\" target=\"_blank\" rel=\"noopener noreferrer\">Gamifics365<\/a> <\/strong>\u2013 Spin the magic of games within Microsoft Dynamics 365 CRM to improve user adoption, enhance productivity, and achieve company goals!<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Recently, we faced an issue in UCI where we had to filter customer field depending on Type of customer. Here, the requirement was as follows: If the type of customer is account then the customer field should show Account entity records only. If the type of customer is contact then the customer field should\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2019\/10\/tip-to-filter-customer-field-depending-on-type-of-customer-in-dynamics-365-uci\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":20648,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[59,60],"tags":[1953,1952],"class_list":["post-20637","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-uci","category-unified-interfaceuci","tag-dynamics-365-uci","tag-unified-interfaceuci"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/20637","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=20637"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/20637\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/20648"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=20637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=20637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=20637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}