Hide Options from Option Set Using JavaScript in Dynamics 365

Hide Options from Option Set Using JavaScript in Dynamics 365

Option Sets (Choice fields) are commonly used in Dynamics 365 to provide users with predefined selections. In many business scenarios, certain options need to be hidden dynamically based on user roles, field values, or business requirements.

JavaScript provides a flexible way to remove option set values at runtime, helping create a cleaner and more user-friendly experience.

Why Hide Option Set Values?

JavaScript Method

Use the removeOption() method to hide specific option set values from a choice field.

function hideOption(executionContext)
{
    var formContext = executionContext.getFormContext();

```
formContext.getControl("new_status")
           .removeOption(100000001);
```

} 

The above code removes the option whose value is 100000001 from the option set control.

Hide Multiple Options

You can remove multiple values by calling removeOption() repeatedly.

function hideOptions(executionContext)
{
    var formContext = executionContext.getFormContext();

```
var control = formContext.getControl("new_status");

control.removeOption(100000001);
control.removeOption(100000002);
control.removeOption(100000003);
```

} 

Hide Options Based on Another Field

A common requirement is to hide option values based on another field's selection.

function filterOptions(executionContext)
{
    var formContext = executionContext.getFormContext();

```
var category =
    formContext.getAttribute("new_category")
               .getValue();

if(category === 100000000)
{
    formContext.getControl("new_status")
               .removeOption(100000002);
}
```

} 
Important: Register the JavaScript function on Form Load and On Change events to ensure option filtering works consistently.

Implementation Steps

  1. Create a JavaScript Web Resource.
  2. Add the script to the form.
  3. Register the function on Form Load.
  4. Pass execution context.
  5. Save and publish customizations.
  6. Test the option filtering behavior.

Common Use Cases

Best Practices

Key Takeaways