CRM Crate

How to add / remove optionset values programmatically in Dynamics 365 CRM?

We will learn to add / remove optionset values programmatically using JavaScript in Dynamics 365 CRM. Before we start, make sure to subscribe to CRM Crate for staying updated in the field of Dynamics 365.

How to add and remove optionset values programmatically in Dynamics 365 CRM?

Why do we need to remove / add optionset values in Dynamics 365?

Consider a business scenario where we need to remove / add a optionset value dynamically according to the dedicated criteria. In the below example, we have two optionset named “Optionset A” and “Optionset B”. Here, we have to filter the values of the “Optionset B” as per the value selected in the “Optionset A”.

When a value “Fruit” is selected in the “Optionset A” then the value “Spinach” and “Ladyfinger” will automatically get removed from the “Optionset B.

Client APIs used for programmatically updating the optionset values

getOption (Client API reference)
Returns an option object with the value matching the argument (label or enumeration value) passed to the method.


removeOption (Client API reference)
Removes an option from a control.


addOption (Client API reference)
Adds an option to a control.

formContext.getControl(arg).addOption(option, index);

JavaScript snippet for removing and adding a optionset value –

Below are the optionset values before execution of the JavaScript in Dynamics 365 form.

JavaScript code for removing and adding the optionset values from field “Optionset B” according to the values present in the field “Optionset A”.

//CRM Crate JavaScript Snippet -
function CallMethod (executionContext) {
            var formContext = executionContext.getFormContext();

//Using getOption control for field "Optionset B".
            var mango = formContext.getAttribute("cc_optionsetb").getOption(1); // Mango = 1
            var banana = formContext.getAttribute("cc_optionsetb").getOption(2); // Banana = 2
            var spinach = formContext.getAttribute("cc_optionsetb").getOption(3); // Spinach = 3
            var ladyfinger = formContext.getAttribute("cc_optionsetb").getOption(4); // Ladyfinger = 4
			//Using removeOption control for removing all the optionset values by default.

//Conditional logic for field "Optionset A".
            var optionsetA = formContext.getAttribute("cc_optionseta").getValue();
			if(optionsetA = 1)
			//Using addOption control for adding optionset value.
			//Using addOption control for adding optionset value.

Below we have validated the result of the above JavaScript snippet in Dynamics 365 CRM form.

Thus we learned to add and remove optionset values programmatically in Dynamics 365 CRM.

5 3 votes
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
Satish Kumar
2 years ago

Thank for this…

1 year ago

on selecting optionseta value as fruit, in optionsetb , the values mango and banana visibled, but vice versa ,on selecting optionsetbvalue as vegitables , in the optionsetb the values spinach and lady finger is not visibled , how to fix this error

1 year ago

how we can remove this select option from this dropdown

Ajay Shinde
16 days ago

How can we achieve it for editable grids fields?

error: CRM Crate Security Engine - Disabled Right Click & Selection!


Well Done,
Welcome to CRM Crate

Stay tuned with us and get all latest updates and learning in Microsoft CRM and related techonologes.