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.
Generate Your Own New Leads Within Microsoft Dynamics 365 CRM
Contact us for a demo to know more about how Maplytics can help you to generate new leads from within Microsoft Dynamics 365 CRM.
Maplytics is a 5-star rated, preferred business app on the Microsoft AppSource that is Certified for Microsoft Dynamics 365 (CfMD) and comes with powerful features like Appointment Planning, Sales Routing, Territory Management, Heat Maps, Geo-analytical Dashboards and more that empower organizations to add more value to their CRM data, improve sales & service processes, and achieve high ROI.
Get your free trial from our Website or Microsoft AppSource!
‘If data is the new oil, location intelligence is ??”
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?
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?
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!
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.
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)(“new_course”);
OptionSetValueCollection courseValues = entity.GetAttributeValue
//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!
How to get an only selected options from Multi-Select Option Set field in Dynamics 365 V9.1 using JavaScript
Hi Rutuja,
You can get all the selected option values from the below line of code.
var selectedOptions = formContext.getControl(“fieldName”).getAttribute().getValue();
or
var selectedOptions = formContext.getAttribute(“fieldName”).getValue();
It will return an array of Selected OptionSet Values, example: [1001,1002,1005].
Hope this helps!
Thanks
My Script is triggering on-Load of Form and I am reading Multi option set field from quick view form below is my code:
quickViewControl = formContext.ui.quickForms.get(“testquickviewcontrol”);
var Test = quickViewControl.getControl(“testmultioptionfield”).getValue();
OR
var Test = quickViewControl.getControl(“testmultioptionfield”).getSelectedValue();
but I am not getting selected values from Multi option set field
Hi,
As mentioned in the earlier reply, you can get all the selected option values from the below line of code:
var selectedOptions = formContext.getControl(“fieldName”).getAttribute().getValue();
or
var selectedOptions = formContext.getAttribute(“fieldName”).getValue();
It will return an array of Selected OptionSet Values, example: [1001,1002,1005].
Thanks