Dynamics 365 v9.0 – Set Multi-Select Option Set values using JScript

Hello everyone,

In my previous post on Multi-Select Option Sets , I mentioned about setting multi-select option set values using setValue() method overwrites existing selected values. In this post I would like to discuss more on that and how we can append or remove certain values using simple JScript methods.

getValue() / getText()- Returns Array

function SetMultiSelValues(executionContext) {
//Get Form Context from Execution Context
var formContext = executionContext.getFormContext();

//Get Array of Selected OptionSet Values
//Returns: [100000005, 100000001]
var selectedValues = formContext.getAttribute("new_multiselect").getValue();

//Get Array of Selected OptionSet Text
//Returns: ["Six", "Two"]
var selectedOptionText = formContext.getAttribute("new_multiselect").getText();
}

setValue() – To Overwrite existing Values

Args: Pass integer or Array of integers

function SetMultiSelValues(executionContext) {
//Set Value a Single Value - Overwrites Existing Selected Values
formContext.getAttribute("new_multiselect").setValue(100000005);

//or

formContext.getAttribute("new_multiselect").setValue([100000005]);

//Set Multiple Values - Overwrites Existing Selected Values
formContext.getAttribute("new_multiselect").setValue([100000003, 100000004]);
}

setValue() – To Append to existing Values

Here I am using concat() JScript method to concatenate existing and new values

function SetMultiSelValues(executionContext) {
//Get Form Context from Execution Context
var formContext = executionContext.getFormContext();

//Get Array of Selected OptionSet Values
//Returns: [100000005, 100000001]
var existingValues = formContext.getAttribute("new_multiselect").getValue();

//Append a set of values
var newValues = [100000003, 100000004];
var updatedValues = ConcatArrays(existingValues, newValues);

//Appends to Existing Selected Values
//New Values: [100000005, 100000001, 100000003, 100000004]
formContext.getAttribute("new_multiselect").setValue(updatedValues);
}
function ConcatArrays(existingValues, newValues) {
if (existingValues === null || Array.isArray(existingValues) === false) {
return newValues;
}

if (newValues === null || Array.isArray(newValues) === false) {
return existingValues;
}
return existingValues.concat(newValues);
}

setValue() – To Remove List of Values

Here I am using JScript’s filter() method to remove values from selected option set values array


function SetMultiSelValues(executionContext) {
//Get Form Context from Execution Context
var formContext = executionContext.getFormContext();

//Get Array of Selected OptionSet Values
//Returns: [100000005, 100000001, 100000003, 100000004]
var existingValues = formContext.getAttribute("new_multiselect").getValue();

//Removes from Existing Selected Values
//New Values: [100000005, 100000004]
var removeValues = [100000001, 100000003];
updatedValues = RemoveFromArray(existingValues, removeValues);
formContext.getAttribute("new_multiselect").setValue(updatedValues);
}

function RemoveFromArray(existingValues, removeValues) {
if (existingValues === null || Array.isArray(existingValues) === false) {
return removeValues;
}

if (removeValues === null || Array.isArray(removeValues) === false) {
return existingValues;
}

return existingValues.filter(function (value, index) {
return removeValues.indexOf(value) == -1;
})
}

Hope it helps..!!

4 thoughts on “Dynamics 365 v9.0 – Set Multi-Select Option Set values using JScript

  1. Pingback: Free Webinar Friday: Dynamics 365 v9: Working with Multi-Select OptionSets | xRM Coaches

Leave a Reply

Your email address will not be published. Required fields are marked *