Multi-Select Option Set field in Dynamics 365 V9.0

By | September 26, 2018

Introduction:

With the release of Dynamics 365 v9.0 Microsoft introduced a new data type i.e. Multi Select Option Set with the help of this we can select more than one option set in Dynamics 365.

Multi-Select Option Set has a list of options and we can select more than one option from the list on selecting checkboxes. i.e we can search option set from the list of option.

In this blog we will discuss how to Get, Set, Add and Remove options in Multi-Select Option Set.

Recently, we came across below scenario,

Where, User wants to update Multi Select Option Set from another option set programmatically and also Ad & Remove options from Multi Select Option Set

  • Retrieve Multi-Select Option Set and copy into another option set programmatically:

Entity entity = _service.Retrieve("account", new Guid(old RecordId), new ColumnSet("new_multioption"));
Entity accountEntity = new Entity ("account", new Guid(New RecordId));

accountEntity["new_multioption"] = entity.GetAttributeValue<OptionSetValueCollection>("new_multioption");
  • Set Multi Select Option Set programmatically:

For Single value

OptionSetValueCollection collectionOptionSetValues = new OptionSetValueCollection();

OptionSetValue optionSet = new OptionSetValue(optionsetValue);

collectionOptionSetValues.Add(optionSet);

entityAccount["new_multioption"] = collectionOptionSetValues;
_service.Update(entityAccount); 

For multiple values

string[] arr = { collection of option set value };

foreach(var item in arr)
{
collectionOptionSetValues.Add(new OptionSetValue(Convert.ToInt32(item)));
}

entityAccount["new_multioption"] = collectionOptionSetValues;
_service.Update(entityAccount);
  • Remove Multi-Select Option Set programmatically:

Remove single value

formContext.getControl(multioptionset).removeOption(optionsetvalue);

Remove multiple values

string[] arr = { collection of option set value };

foreach(var item in arr)
{
	formContext.getControl(multioptionset).removeOption(item);
}
  • Add new options in Multi Select Option Set programmatically:

Add Single option value

formContext.getControl(multioptionset).addOption(new Xrm.OptionSetItem(value, text), position);

Add Multiple option value

var position = 0;
var optArr = [Collection of option set ];

for (var i = optArr.length - 1; i >= 0; i--) {
formContext.getControl(multioptionset).addOption(new Xrm.OptionSetItem(optArr [i].value, optArr [i].text), position++);
	}

Conclusion:

Using the code above user can Get, Set, Add and Remove Multi Select Option Set programmatically in Dynamics 365 v9.0.

Improve-the-user-adoption-of-Dynamics-365-by-monitoring-user-activity-within-CRM

2 thoughts on “Multi-Select Option Set field in Dynamics 365 V9.0

  1. Kiran

    Hello

    While trying to remove couple of values using “removeoption” in multi option sets during onChange of another field I think I found a bug in the control
    The values get removed from the multi option set but does not actually reflect in the UI. Those options are still visible but cannot be selected. If I try “Select All” only the values which I didn’t remove get selected. Can you please let me know if you also encountered this issue and suggest any work around?

    Reply
    1. inogic Post author

      Hi,

      Yes, I did encounter the issue and below is workaround for the same:

      When we remove SelectedOptionset value in MultiOptionset, at that time it actually gets removed from the list but not from form UI, for that we have to clear selected option set value first and then remove list of option from Multi Option Set.

      Find the below code to clear selected options value in Multi option set.
      formContext.getAttribute(“new_multioptionset”).setValue(null); // is used to clear selected options on multi optionset

      Hope this helps and feel free to ask in case you have any query regarding the same.

      Thanks!

      Reply

Leave a Reply

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