How to Set Optionset Values Based On Related Optionset Value?

How to Set Optionset Values Based On Related Optionset Value?

In this article, we show how a user can set an option set of a field based on the filtration of another option set’s selected value.

We have a Filter Option Set section. In that, we have two custom option set fields: Independent Option Set and Dependent Option Set.

We have a Filter Option Set section

Step 1: Create two option sets.

1. Independent Option Set.

2. Dependent Option Set.

Create two option sets

Create two option sets-2

Step 2: Write JavaScript as per the requirement.

Here we have written the code, which will be called on the change of the Independent option, and based on its selected value user can set the options in another field (Dependent option).

function OptionSetAddRemove(executionContext) {
formContext = executionContext.getFormContext();
var independentOptionSetValues = formContext.getAttribute("new_independentoption").getValue()
var dependentOptionSetValues = formContext.ui.controls.get("new_dependentoption");
var optionSetValues = dependentOptionSetValues.getAttribute().getOptions();
dependentOptionSetValues.clearOptions();
optionSetValues.forEach(function (element) {
if (independentOptionSetValues == "100000000") {
if (element.value == 100000000 || element.value == 100000004)
dependentOptionSetValues.addOption(element);
}
if (independentOptionSetValues == "100000001") {
if (element.value == 100000001)
dependentOptionSetValues.addOption(element);
}
if (independentOptionSetValues == "100000002") {
if (element.value == 100000003 || element.value == 100000001)
dependentOptionSetValues.addOption(element);
}
if (independentOptionSetValues == "100000003") {
if (element.value == 100000002)
dependentOptionSetValues.addOption(element);
}
}
);
}

Step 3: Now Click on Settings Icon → Select Advanced Settings.

Select Advanced Settings

Step 4: Click on Settings → Select Solutions.

Click on Settings Select Solutions

Step 5: Select the appropriate solution.

Select the appropriate solution

Step 6: After that, click on Web Resource → Select New.

Web Resource Select New

Step 7: Then Give the Appropriate name to Webresource select Type(JScript) and Browse File.

Once it’s done click on Save and Publish.

Appropriate name to Webresource select Type

Step 8: Go to Entities → Expand Entity → Click on Form → Select Main Type form.

Expand Entity Click on form

Step 9: Go to Form → Form Properties → Add new Library → Click New.

Form Properties Add new Library

Step 10: Click on Add. It will open LookUp Record Dialog Box.

LookUp Record Dialog Box

Step 11: From LookUp Record Dialog Box, select Appropriate Web Resource and click on Add.

LookUp Record Dialog Box select Appropriate Web

Step 12: After that in the form properties Event Handles Section select Control Independent Option and Event OnChange and Add Web Resource. Click on Add button it will open the Handler Properties dialog box.

Handles Section select Control Independent

Step 13: In Handler Properties, give the Function name for which Function you want to call at onChange of Independent Option.

Click on OK.

Change of Independent Option

Step 14: Save and Publish the changes.

Step 15: Open the record of the entity in which you have done configuration to set option set based on the selection of another option set.

For example, in the above code, we have configured that if the Independent option user selects Guitar, then it will set only Red as the option set in the Dependent option set.

Here the user selects Guitar, then only Red Color Option in the Dependent option shows.

User selects Guitar Red Color Option

User selects Guitar Red Color Option-2

So this is how a user can change in form properties and change values of the option set based on the selection of another optionset.

All product and company names are trademarks™, registered® or copyright© trademarks of their respective holders. Use of them does not imply any affiliation with or endorsement by them.

Riddhi Faldu

Riddhi Faldu

Riddhi is a firm believer of serenity and writing is something that keeps her grounded. Being a Computer Engineer by qualification, understanding and writing about tech comes to her naturally. It wouldn't be wrong to call her diligent as she likes pushing herself and others to bring the best on table. In leisure time, she likes practicing calligraphy and wishes to master the beautiful concoction of ink and words.

Read Related Blogs About Dynamics 365 Portal

How to Clear the Cache in the Power Apps Portal

How to Clear the Cache in the Power Apps Portal

3 Min
Revolutionizing Financial Services with Dynamics 365 Customer Portal

Revolutionizing Financial Services with Dynamics 365 Customer Portal

5 Min
Why You Need D365 Developers in Your Team

Why You Need D365 Developers in Your Team

4 Min
To Top