Bhubaneswar, Odisha, India
+91-8328865778
support@softchief.com

Add or Remove items options from Multi-select optionset field (Choices) in Dynamics 365 power apps using javascript

Add or Remove items options from Multi-select optionset field (Choices) in Dynamics 365 power apps using javascript

In this article you will see how you can add or remove dynamically optionset values in a multi-select optionset or choices data type field in dynamics 365 power apps.

Lets take a scenario: you have two fields one is Gender Choice (Male, Female) (Single select optionset) and another is Degree Choices (MCA, MBA, BTech, Bsc) (multiselect optionset). If Male Gender selected only allow MCA and MBA in Degree options otherwise display all items.

To do this, write a JS web resource and use the following code.

function setDegrees(context)
{
	var formContext = context.getFormContext();
	
	var gender = formContext.getAttribute("soft_gender").getValue();
	
	//applicable to choice and choices field
	var arrDegree = formContext.getAttribute("soft_degrees").getOptions();

    //setup raw optionsets
	var mca = {value : 408260000, text : "MCA"};
	var mba = {value : 408260001, text : "MBA"};
	var btech = {value : 408260002, text : "B. Tech."};
	var bsc = {value : 408260003, text : "B. Sc."};

    // Clear current items
	  for (var i = 0; i < arrDegree.length; i++) {
		formContext.getControl("soft_degrees").removeOption(arrDegree[i].value);
	  }
	  

    if(gender == 408260000) 
	{
		//ad mca and mba
		formContext.getControl("soft_degrees").addOption(mca);
		formContext.getControl("soft_degrees").addOption(mba);
	}
	else
	{
		//ad mca and mba
		formContext.getControl("soft_degrees").addOption(mca);
		formContext.getControl("soft_degrees").addOption(mba);
		formContext.getControl("soft_degrees").addOption(btech);
		formContext.getControl("soft_degrees").addOption(bsc);
	}
}

call it on change of Gender.

Now publish and test If you select male as gender you will see only two options.

if you select any other option instead of Male then you will see all otpions.

Hope this helps.

Follow my blog for more trending topics on Dynamics 365, Azure, C#, Power Portals and Power Platform. For training, Courses and consulting, call to us at +91 832 886 5778 I am working more for community to share skills in Dynamics 365 and Power Platform. Please support me by subscribing my YouTube Channel. My YouTube Channel link is this : https://www.youtube.com/user/sppmaestro