{"id":15279,"date":"2019-04-01T11:37:20","date_gmt":"2019-04-01T11:37:20","guid":{"rendered":"https:\/\/www.inogic.com\/blog\/?p=15279"},"modified":"2019-04-01T11:37:20","modified_gmt":"2019-04-01T11:37:20","slug":"cascading-of-two-multi-select-option-set-fields-in-dynamics-365-v9-0","status":"publish","type":"post","link":"https:\/\/www.inogic.com\/blog\/2019\/04\/cascading-of-two-multi-select-option-set-fields-in-dynamics-365-v9-0\/","title":{"rendered":"Cascading of Two Multi-Select Option Set fields in Dynamics 365 v9.0"},"content":{"rendered":"<h2><strong>Introduction<\/strong><\/h2>\n<p>Microsoft introduced Multi Select Option Set in Dynamics 365 V9.0, which will help us to select more than one option.<\/p>\n<p>Recently we had a business requirement, where the client required multi-select option set values to populate programmatically on selection of another multi-select option set.<\/p>\n<p>To achieve this, first, we need to create two multi-select option set fields in CRM e.g. Mobile Company (Parent Multi-Select option set) and Mobile Handset (Child Multi-Select option set).<\/p>\n<p>Then we need to create one JSON web resource in Dynamics 365 CRM, where we can define child option set values against each parent option set value.<\/p>\n<h2><strong>JSON<\/strong><\/h2>\n<p>E.g.<\/p>\n<p>var JsonDataobj =<\/p>\n[{<\/p>\n<p>mobileCompany: 100000010,<\/p>\n<p>mobileHandset: [<\/p>\n<p>{value: 100000011,text: &#8216;Nokia 6.1&#8217;},<\/p>\n<p>{value: 100000012,text: &#8216;Nokia 7.1&#8217;},<\/p>\n<p>{value: 100000013,text: &#8216;Nokia 8.1&#8217;}<\/p>\n<p>]},<\/p>\n<p>{<\/p>\n<p>mobileCompany: 100000020,<\/p>\n<p>mobileHandset: [<\/p>\n<p>{value: 100000021,text: &#8216;iPhone X&#8217;},<\/p>\n<p>{value: 100000022,text: &#8216;iPhone 7&#8217;}<\/p>\n<p>]},<\/p>\n<p>{<\/p>\n<p>mobileCompany: 100000030,<\/p>\n<p>mobileHandset: [<\/p>\n<p>{value: 100000031,text: &#8216;Samsung S8&#8217;}<\/p>\n<p>]\n<p>}]\n<p>Now onLoad of Entity Form and onChange of parent Multi-Select option set we need to call below script.<\/p>\n<h2><strong>Script<\/strong><\/h2>\n<p>cascadingMultiOptionSet(executionContext): void {<\/p>\n<p>\/\/\/ &lt;summary&gt;<\/p>\n<p>\/\/\/\u00a0 This function trigger on onload of form and onchange of Parent multi-option set field and set values of child multi option set field<\/p>\n<p>\/\/\/ &lt;\/summary&gt;<\/p>\n<p>\/\/\/ &lt;returns type=&#8221;void&#8221; \/&gt;<\/p>\n<p>var functionName = &#8220;cascadingMultiOptionSet&#8221;;<\/p>\n<p>var newChildOptArr = [];<\/p>\n<p>var position = 0;<\/p>\n<p>var newSelectedChildOpts = new Array();<\/p>\n<p>try {<\/p>\n<p>\/\/JSON data retrieved from webresource<\/p>\n<p>var parentChildCollection = JsonDataobj;<\/p>\n<p>var formContext = executionContext.getFormContext();<\/p>\n<p>\/\/get parent&#8217;s selected\u00a0 option values<\/p>\n<p>var selectedParentOptValue = formContext.getAttribute(&#8220;ikl_mobilecompanies&#8221;).getValue();<\/p>\n<p>\/\/check if value selected or not<\/p>\n<p>if (selectedParentOptValue != null &amp;&amp; selectedParentOptValue.length &gt; 0) {<\/p>\n<p>\/\/add value from parentChildCollection array to newChildOptArr.<\/p>\n<p>for (var i = 0; i &lt; selectedParentOptValue.length; i++) {<\/p>\n<p>newChildOptArr = newChildOptArr.concat((parentChildCollection.find(x =&gt; x.mobileCompany == selectedParentOptValue[i])).mobileHandset);<\/p>\n<p>}<\/p>\n<p>\/\/get all options from child option set<\/p>\n<p>var existingChildOptValues = formContext.getControl(&#8220;ikl_mobilehandset&#8221;).getOptions();<\/p>\n<p>\/\/get selected options from child optionset<\/p>\n<p>var selectedChildOptValue = formContext.getAttribute(&#8220;ikl_mobilehandset&#8221;).getValue();<\/p>\n<p>var childOptCount = existingChildOptValues.length;<\/p>\n<p>\/\/remove options from child optionset<\/p>\n<p>for (var i = childOptCount &#8211; 1; i &gt;= 0; i&#8211;) {<\/p>\n<p>\/\/check if option is not selected or not present in newChildOptArr array then remove that option<\/p>\n<p>if (selectedChildOptValue == null || selectedChildOptValue.indexOf(parseInt(existingChildOptValues[i].value)) &lt;= -1 || (newChildOptArr.find(x =&gt; x.value == existingChildOptValues[i].value)) == undefined) {<\/p>\n<p>\/\/remove options from child options set<\/p>\n<p>formContext.getControl(&#8220;ikl_mobilehandset&#8221;).removeOption(existingChildOptValues[i].value);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>\/\/set position of new element<\/p>\n<p>position = selectedChildOptValue == null ? 0 : selectedChildOptValue.length;<\/p>\n<p>\/\/clear selected values from child option set<\/p>\n<p>formContext.getAttribute(&#8220;ikl_mobilehandset&#8221;).setValue(null);<\/p>\n<p>\/\/add new options from newChildOptArr to child option set field<\/p>\n<p>for (var i = 0; i &lt; newChildOptArr.length; i++) {<\/p>\n<p>\/\/if value aready selected then don&#8217;t add that value<\/p>\n<p>if (selectedChildOptValue == null || selectedChildOptValue.indexOf(parseInt(newChildOptArr[i].value)) &lt;= -1) {<\/p>\n<p>\/\/addOptions with values and position<\/p>\n<p>var optionSet = new Object();<\/p>\n<p>optionSet.value = newChildOptArr[i].value;<\/p>\n<p>optionSet.text = newChildOptArr[i].text;<\/p>\n<p>formContext.getControl(&#8220;ikl_mobilehandset&#8221;).addOption(optionSet, position++);<\/p>\n<p>}<\/p>\n<p>\/\/add previously selected option set to newSelectedChildOpts array<\/p>\n<p>if (selectedChildOptValue != null &amp;&amp; selectedChildOptValue.indexOf(parseInt(newChildOptArr[i].value)) &gt; -1) {<\/p>\n<p>newSelectedChildOpts = newSelectedChildOpts.concat(parseInt(newChildOptArr[i].value, 10));<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>else\/\/if parent options not selected then clear child options<\/p>\n<p>{<\/p>\n<p>formContext.getAttribute(&#8220;ikl_mobilehandset&#8221;).setValue(null);<\/p>\n<p>formContext.getControl(&#8220;ikl_mobilehandset&#8221;).clearOptions();<\/p>\n<p>}<\/p>\n<p>\/\/set value of selected child option set<\/p>\n<p>if (newSelectedChildOpts != null &amp;&amp; newSelectedChildOpts.length &gt; 0) {<\/p>\n<p>formContext.getAttribute(&#8220;ikl_mobilehandset&#8221;).setValue(newSelectedChildOpts);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<p>catch (ex) {<\/p>\n<p>throw new Error(ex);<\/p>\n<p>}<\/p>\n<p>}<\/p>\n<h2><strong>Result<\/strong><\/h2>\n<p><strong>1)<\/strong> If we select Nokia in Mobile Company Multi-select option set, then the Mobile Handset will bind only mobile handsets of Nokia.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-15280\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/04\/1Cascading-of-Two-Multi-Select-Option-Set-fields-in-Dynamics-365.png\" alt=\"Cascading of Two Multi-Select Option Set fields in Dynamics 365\" width=\"1344\" height=\"391\" \/><\/p>\n<p><strong>2)<\/strong> If we select Apple and Nokia in Mobile Company, then both Apple and Nokia handset will be displayed in Mobile Handsets multi-select option set and the selected values of Nokia will remain the same.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-15281\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/04\/2Cascading-of-Two-Multi-Select-Option-Set-fields-in-Dynamics-365.png\" alt=\"Cascading of Two Multi-Select Option Set fields in Dynamics 365\" width=\"1375\" height=\"469\" \/><\/p>\n<p><strong>3)<\/strong> If we remove Nokia from Mobile Company, then only Apple company handsets will be displayed in Mobile Handset multi-select option set and the selected options of Nokia handset will be cleared.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-15282\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/04\/3Cascading-of-Two-Multi-Select-Option-Set-fields-in-Dynamics-365.png\" alt=\"Cascading of Two Multi-Select Option Set fields in Dynamics 365\" width=\"1379\" height=\"384\" \/><\/p>\n<p><strong>4)<\/strong> If the Mobile Company doesn\u2019t contain any value, then Mobile Handset multi-select option will remain empty.<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-full wp-image-15283\" style=\"border: 1px solid #0a0a0a; padding: 1px; margin: 1px;\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/04\/4Cascading-of-Two-Multi-Select-Option-Set-fields-in-Dynamics-365.png\" alt=\"Cascading of Two Multi-Select Option Set fields in Dynamics 365\" width=\"1386\" height=\"258\" \/><\/p>\n<p>In order to see saved values of the multi-select option set for both parent and child it is necessary to trigger the above script function on a load of form.<\/p>\n<h2><strong>Conclusion<\/strong><\/h2>\n<p>We can achieve cascading of two multi-select option set from the above given script.<\/p>\n<p><a href=\"https:\/\/www.inogic.com\/product\/productivity-pack\/click-2-clone-microsoft-dynamics-crm-records\" target=\"_blank\" rel=\"noopener noreferrer\"><img decoding=\"async\" class=\"aligncenter  wp-image-15285\" src=\"https:\/\/www.inogic.com\/blog\/wp-content\/uploads\/2019\/04\/Click2Clone-One-Click-Productivity-App-to-Copy_clone-Dynamics-365_CRM-Records.png\" alt=\"Click2Clone-One-Click-Productivity-App-to-Copy_clone-Dynamics-365_CRM-Records\" width=\"828\" height=\"207\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction Microsoft introduced Multi Select Option Set in Dynamics 365 V9.0, which will help us to select more than one option. Recently we had a business requirement, where the client required multi-select option set values to populate programmatically on selection of another multi-select option set. To achieve this, first, we need to create two multi-select\u2026 <span class=\"read-more\"><a href=\"https:\/\/www.inogic.com\/blog\/2019\/04\/cascading-of-two-multi-select-option-set-fields-in-dynamics-365-v9-0\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":13,"featured_media":15284,"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":[13,16,18,19],"tags":[],"class_list":["post-15279","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-customizations","category-dynamics-365","category-dynamics-365-v9-2","category-dynamics-crm"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/15279","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=15279"}],"version-history":[{"count":0,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/posts\/15279\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media\/15284"}],"wp:attachment":[{"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/media?parent=15279"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/categories?post=15279"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.inogic.com\/blog\/wp-json\/wp\/v2\/tags?post=15279"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}