Showing posts with label Multi Select OptionSet. Show all posts
Showing posts with label Multi Select OptionSet. Show all posts

Thursday, November 5, 2020

Dynamics 365 - Dynamically Add / Remove Values from a Multi-select Picklist

Following code can be used to dynamically remove a particular value from a multi-select option set:


function RemoveValuefromMultiSelectOptionSet(executionContext, valueInteger)

{

var formContext = executionContext.getFormContext();

formContext.ui.controls.getByName("new_multiselectpicklist1").removeOption(parseInt(valueInteger,10));


}


Similarly, in case a value needs to be added dynamically (or be brought back according to context), following code can be used: 


function AddValuefromMultiSelectOptionSet(executionContext, valueText, valueInteger)

{

var formContext = executionContext.getFormContext();

var newObj = { "text" : valueText, "value" : parseInt(valueInteger,10)};

formContext.ui.controls.getByName("new_picklist1").addOption(newObj);

}

Dynamics 365 - How to Get Selected Count of Multi-Select Picklist

Lets take an example of a multi-select picklist having 5 values: 

  1. Banana
  2. Orange
  3. Strawberry
  4. Guava 
  5. Grapes
While working on the form, if a user selects only 2 out of these, e.g: 

  1. Banana
  2. Guava

Following JavaScript will return the count of selected values in the multi-select picklist i.e. 2

function GetMultiSelectPicklistLength(executionContext)
{
var length = 0;
var formContext = executionContext.getFormContext();
var multiSelectField = formContext.getAttribute("new_MultiselectPicklist");
if(multiSelectField != null)
{
var fieldValue = multiSelectField.getText();
if(fieldValue != null)
{
lengthValue = fieldValue.length;
}
}
return length;
}


Dynamics 365 - Dynamically Add / Remove Values from a Multi-select Picklist

Following code can be used to dynamically remove a particular value from a multi-select option set: function RemoveValuefromMultiSelectOptio...