How to work with MultiSelect Option Set Fields in Dynamics 365 v9

One of them is the MultiSelect Option Set with attributes of the type OptionSetValueCollection. This makes it possible to select more than one value of a picklist and additionally to search for individual options or a combination of these. The option to search for values can help, as you can enter up to 150 options in this field. All selected values separated by semicolons and displayed in alphabetical order. In Figure 1you can see the representation of this field type.

Adding this field to an entity works as before. It is simply a new field type to select. Here you can insert options as usual. It is also possible to use one global optionset for both, a MultiSelect Option Set and a simple Option Set. There are still small differences. For example, there is no way to set a default value during creation.

 

Advanced Find

You can use the MultiSelect Option Set Field as usual in the search query. If the operators “equal” or “not equal” are used it only searches for records which exactly fit. In Figure 1 is “abstract thinking;patience” selected. I can find this record if I search for exact this combination but not if I search for “equals abstract thinking, patience, logic” or for “equals patience”.

As you can see, it is important to choose the right operator for your search. If you do not want an exclusive match it is better to use the operators “contain values/not contain values”.

 

JavaScript and MultiSelect Option Sets

First, you have to know that there is a new way to communicate with the CRM via JavaScript. That means XRM.Page is set deprecated by Microsoft. The new option uses getFormatContext(). When adding a JavaScript to the form, it is necessary to check the option “Pass execution context as first parameter” in the form editor. As you can see in Code Snippet 1 the executionContext is passed to the onLoad function. Now we can get the format context, here called context. If you now want to retrieve the contents of a MultiSelect option Set field we can use the method getAttribute().getValue() as usual. However, the return value is an array of optionset values. Here we get just the Optionset value. If we want to get the names of the options, again as an array, we can use the method getAttribute().getText(). Both functions called via the context. They can now be treated completely like arrays.

 

function onLoadMplTest(executionContext) {
          if(executionContext === undefined) throw "No execution context existing.";
             var context = executionContext.getFormContext();
             var mplValues = context.getAttribute("rit_programmingskills_mpl").getValue();
             var mplValuesAsText =
             context.getAttribute("rit_programmingskills_mpl").getText(); } }

 

Moreover, how to set values with JavaScript? It is possible to pass the optionset value to the setValue() method. You can also pass an array of these values. You can see this as an example in Code Snippet 2.

 

//Pass integer or integer array

    //Set single value

    context.getAttribute("rit_programmingskills_mpl").setValue(108000000);

    context.getAttribute("rit_programmingskills_mpl").setValue([108000000]);

    //Set multiple values

    context.getAttribute("rit_programmingskills_mpl").setValue([108000000, 108000001]);

 

If you now want to append values to the list just use the concat() method that JavaScript offers to adjust the array before setting the values.

Do you have the intention to remove values from the field it refers, as just described, to an adaption of the array. JavaScript has the method filter() which exactly is there to remove values from arrays.

 

C# Plugins and MultiSelect Option Sets

So how does it work with a C# plugin? I just want to give you a short view. To get the values we need to get the associated entity. I will call it myEntity. We can get the attributes as an OptionSetValueCollection as you can see in Code Snippet 3. The variable myFieldName must be a MultiSelect Option Set field. Then you can query all values with a foreach loop.

 

OptionSetValueCollection values =
(OptionSetValueCollection)myEntity.Attributes[myFieldName];
foreach (var option in values)
{
int value = option.Value;
}

 

To set values just create an own OptionSetValueCollection and add OptionSetValues. Take care to use the integer values. This collection can then be assigned to the corresponding field as usual and updated or created by the IOrganizationService.

Beitrag teilen: