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

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

  1. Avik Banerjee

    Yes it is great to see multi select option set.
    Somehow it looks half cooked to me,as per my research there isn’t any way to build views/dashboards using these fields.

    Anyone with a difference in opinion?

    Reply
  2. 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
  3. Shunmuga

    I added a multi select option set field in form in which it will populate values from entity
    (particular field).But when I select the option it is not updating in the control.It is still blank.

    Reply
    1. Inogic Post author

      Please make sure there is no other exception in your code which might have caused this behaviour. We are able to retrieve and set values in multi optionset fields in C#.

      To replicate your scenario, I created two multioption set fields on Account entity in my trial environment. And I created a plug-in which triggered on change of a text field in Account entity. That plug-in retrieved values selected in the first multi optionset field(Course field) and set the same in the second multi optionset field(Schedule Course). And we were able to see same values in both multioption set fields as seen in the below screenshot:

      Given below is a part of code for your reference.

      //retrieving the values selected in the multi optionset field Course(logical name-new_course)
      OptionSetValueCollection courseValues = entity.GetAttributeValue(“new_course”);

      //checking the count of values selected in the Course field
      tracing.Trace(“Checking the collection count: ” + courseValues.Count);

      OptionSetValueCollection scheduleCourseValues = new OptionSetValueCollection();
      foreach (var course in courseValues)
      {
      //adding each value present in the course field optionset collection to schedulecourse optionset collection
      scheduleCourseValues.Add(new OptionSetValue(Convert.ToInt32(course.Value)));

      }

      Entity account = new Entity(“account”);
      account.Id = entity.Id;
      //setting the multi optionset field Schedule Course(logical name-new_schedulecourse) with //schedulecourse optionset collection
      account[“new_schedulecourse”] = scheduleCourseValues;
      service.Update(account);

      Hope this helps.

      Thanks!

      Reply

Leave a Reply

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