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.
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)
formContext.getAttribute(arg).getOption(value)
Returns an option object with the value matching the argument (label or enumeration value) passed to the method.
removeOption (Client API reference)
formContext.getControl(arg).removeOption(value);
Removes an option from a control.
addOption (Client API reference)
formContext.getControl(arg).addOption(option, index);
Adds an option to a control.
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. formContext.getControl("cc_optionsetb").removeOption(1); formContext.getControl("cc_optionsetb").removeOption(2); formContext.getControl("cc_optionsetb").removeOption(3); formContext.getControl("cc_optionsetb").removeOption(4); //Conditional logic for field "Optionset A". var optionsetA = formContext.getAttribute("cc_optionseta").getValue(); if(optionsetA = 1) { //Using addOption control for adding optionset value. formContext.getControl("cc_optionsetb").addOption(mango); formContext.getControl("cc_optionsetb").addOption(banana); } else { //Using addOption control for adding optionset value. formContext.getControl("cc_optionsetb").addOption(spinach); formContext.getControl("cc_optionsetb").addOption(ladyfinger); } }
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.
Thank for this…
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
how we can remove this select option from this dropdown
Hi Komal,
The “Select” is denoted for null or empty value. Therefore, we cannot remove this option.
Thanks
How can we achieve it for editable grids fields?