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

You can enroll now !We are giving 30% discount on our Internship Program

Don’t miss the chance to participate in the upcoming Internship Program which will be done using Microsoft Dot Net Web Development Full Stack Technology. The new batch will be starting from May 20, 2024.  We will have most experienced trainers for you to successfully complete the internship with live project experience.

Why to choose Our Internship Program?

Industry-Relevant Projects
Tailored Assignments: We offer projects that align with your academic background and career aspirations.
Real-World Challenges: Tackle industry-specific problems and contribute to meaningful projects that make a difference.

Professional Mentorship
Guidance from Experts: Benefit from one-on-one mentorship from seasoned professionals in your field.
Career Development Workshops: Participate in workshops that focus on resume building, interview skills, and career planning.

Networking Opportunities
Connect with Industry Leaders: Build relationships with professionals and expand your professional network.
Peer Interaction: Collaborate with fellow interns and exchange ideas, fostering a supportive and collaborative environment.

Skill Enhancement
Hands-On Experience: Gain practical skills and learn new technologies through project-based learning.
Soft Skills Development: Enhance communication, teamwork, and problem-solving skills essential for career success.

Free Demo Class Available