JavaScript to Copy a MultiSelect Option Set Choices field

Microsoft Dynamics 365 allows a table column (field) type that supports selection of multiple option values (choices). This is a very good field format type to setup vs creating custom “one to many” or “many to many” table for storing multiple values associated with a record. An example would be an Account record with the various types of wine it carries such as Cabernet Sauvignon, Pinot Noir, Merlot, etc. (I’m working in the luxury brand winery CRM these days so this example readily comes to mind…)

At present, Multi-Select Choices field is not supported via Business Rules or legacy Workflows (The good news is that Power Automate Flows could access and update multi-selection picklist option sets in async mode). So if we need some custom logic via the Front End User Interface – what should we do?

We will have to use Power Apps Client Side JavaScript since legacy Microsoft CRM’s Real Time Workflow is not even an option.

Below is a simple JavaScript example of accessing and copying a Multi-Select Choices field value to another Choices field:

Here are the steps to setup the example:

// jsCRM220501.copyChoices01
var jsCRM220501 = window.jsCRM220501 || {};
(function () {
  "use strict";
  this.copyChoices01 = function (executionContext) {
    var formContext = executionContext.getFormContext();
    var mpl001Values = formContext.getAttribute("new_choices001").getValue();
    formContext.getAttribute("new_choices002").setValue(mpl001Values)
  }
}).call(jsCRM220501);

Setup two MultiSelect Option Set (Choices) fields on the Account form. Name these two MultiSelect Option Set fields “Choices001” and “Choices002” so that it matches the sample code field name references above. Otherwise be sure to match the field names by updating the “new_choices001” and “new_choices002” appropriately.

Setup a JavaScript Web Resource (it is better to use the Classic Solution Designer for this at this time)

Click on the Text Editor button on the left of the Type field and insert the JavaScript code above

Save and Publish this Web Resource

Set the Account Form Onload Event to call this Web Resource (Onload Event is use for this example – could be setup for other events such as field update or record save event)

Save the Form and Publish the Form. That’s it for this simple Multi Choices JavaScript copy example.

Here is a Multi Choices JavaScript example to show a hidden form field if one of the multi selected option set value is 10000001:

var jsCRM220501 = window.jsCRM220501 || {};
(function () {
  "use strict";
  this.displayField = function (executionContext, fieldname) {
    var formContext = executionContext.getFormContext();
    var mpl001Values = formContext.getAttribute("new_choices001").getValue();
    if (mpl001Values) {
      // check if one of the value selected is 100000001
      if (mpl001Values.includes(100000001)) {
        // hide the new_textfield001 field on crm form if 100000001 is selected 
        formContext.getControl("new_textfield001").setVisible(true);
      }
    } 
  }
}).call(jsCRM220501);

Here is a Multi Values JavaScript example to remove a selected (de-select) value 10000002:

// jsCRM220501.copyChoices01
var jsCRM220501 = window.jsCRM220501 || {};
(function () {
  "use strict";
  this.copyChoices01 = function (executionContext) {
    var formContext = executionContext.getFormContext();
    var mpl001Values = formContext.getAttribute("new_choices001").getValue();
    // deselect value 100000002 before copy to another multi-select picklist
    mpl001Values = mpl001Values.filter(function(e) { return e !== 100000002 })
    formContext.getAttribute("new_choices002").setValue(mpl001Values)
  }
}).call(jsCRM220501);

Contact me if you need some Microsoft Dynamics 365 / Power Apps / CRM Sales Customer Engagement assistance.

Frank Lee
12 times awarded Microsoft MVP – Dynamics 365 / CRM
San Francisco Bay Area | Silicon Valley

About Frank Lee

Microsoft Dynamics 365 CRM/xRM consultant in San Francisco, USA.  Awarded the Microsoft MVP (Dynamics 365/CRM) 12 consecutive years from 2006 to 2018. Actively involved with Microsoft Dynamics CRM implementations since Microsoft CRM v1.0 beta (2002). Super passionate about everything CRM, especially in the areas of Cloud Computing, A.I., Digital Transformation and Automation.
This entry was posted in Dynamics 365 and tagged , , , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s