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.

formContext.getAttribute(arg).getOption(value)

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

formContext.getControl(arg).removeOption(value);

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.
			            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.

5 2 votes
Article Rating
Subscribe
Notify of
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Satish Kumar
4 months ago

Thank for this…

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

Congratulations!

Well Done,
Welcome to CRM Crate

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

Practise EXams

Free!!

Sharpen your skills with our free practise exams.