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 are 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:
// 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);
Here are the steps to setup the above:
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 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 next to Type and insert the 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)
That’s it for this simple Multi Choices JavaScript copy example.
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
This was originally posted here.
*This post is locked for comments