Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

option set value if lookup field contain data

(0) ShareShare
ReportReport
Posted on by

Hello experts,

I want to show/Hide option set value options based on lookup fields.

For Example: 

Option set " Option Set " has options " A,B,C,D,E,F",

and there are three lookup fields Field 1, Field 2 and Field 3.

If only Field 1 contains Data : options should be A, B 

If only Field 2 contains Data : options should be C, D

If only Field 3 contains Data : options should be E, F

Only one field will contain data at a time.

*This post is locked for comments

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at
    RE: option set value if lookup field contain data

    Hi Sandeep,

    Try to write something like below , I am considering each time you have only one lookup contains data , you may change your condition.

    // Register the function onChange  lookup .
            function OptionSetAddRemove() {
                var optionSet = Xrm.Page.ui.controls.get("new_myoptionset");
                var optionSetValues = optionSet.getAttribute().getOptions();
                optionSet.clearOptions();
                optionSetValues.forEach(function (element) {
                    if (Xrm.Page.getControl("Field1").getAttribute().getValue() != null && Xrm.Page.getControl("Field2").getAttribute().getValue() == null && Xrm.Page.getControl("Field3").getAttribute().getValue() == null) {
                        if (element.value == "831260006") //A Value  is  831260006
                            optionSet.addOption(element);
                        if (element.value == "831260007")  // B value is 831260007
                            optionSet.addOption(element);
    
                        if (element.value == "831260008")  // C value is 831260008
                            optionSet.removeOption(element);
    
                        if (element.value == "831260009")  // D value is 831260009
                            optionSet.removeOption(element);
                        if (element.value == "831260010")  // E value is 831260010
                            optionSet.removeOption(element);
                        if (element.value == "831260011")  // F value is 831260010
                            optionSet.removeOption(element);
                    }
                    if (Xrm.Page.getControl("Field2").getAttribute().getValue() != null && Xrm.Page.getControl("Field1").getAttribute().getValue() == null && Xrm.Page.getControl("Field3").getAttribute().getValue() == null) {
                        if (element.value == "831260006") //A Value  is  831260006
                            optionSet.removeOption(element);
                        if (element.value == "831260007")  // B value is 831260007
                            optionSet.removeOption(element);
    
                        if (element.value == "831260008")  // C value is 831260008
                            optionSet.addOption(element);
    
                        if (element.value == "831260009")  // D value is 831260009
                            optionSet.addOption(element);
                        if (element.value == "831260010")  // E value is 831260010
                            optionSet.removeOption(element);
                        if (element.value == "831260011")  // F value is 831260010
                            optionSet.removeOption(element);
                    }
                    if (Xrm.Page.getControl("Field3").getAttribute().getValue() != null && Xrm.Page.getControl("Field1").getAttribute().getValue() == null && Xrm.Page.getControl("Field2").getAttribute().getValue() == null) {
                        if (element.value == "831260006") //A Value  is  831260006
                            optionSet.removeOption(element);
                        if (element.value == "831260007")  // B value is 831260007
                            optionSet.removeOption(element);
    
                        if (element.value == "831260008")  // C value is 831260008
                            optionSet.removeOption(element);
    
                        if (element.value == "831260009")  // D value is 831260009
                            optionSet.removeOption(element);
                        if (element.value == "831260010")  // E value is 831260010
                            optionSet.addOption(element);
                        if (element.value == "831260011")  // F value is 831260010
                            optionSet.addOption(element);
                    }
    
                });
            }


  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at
    RE: option set value if lookup field contain data

    Hi,

    Please try below code,

    	function OptionSetValues()
    	{
    
    	 var attribute = Xrm.Page.data.entity.attributes.get("optionsetschemaname");
    	 
    	  var options = attribute.getOptions();
    		var control = Xrm.Page.getControl("optionsetschemaname")
    		control.clearOptions();
    		if (Xrm.Page.data.entity.attributes.get("field1schemaname").getValue() != null) {
    			 control.addOption(options[1]);// optionset value for A
    		 control.addOption(options[2]);// optionset value for B
    		}
    		else if (Xrm.Page.data.entity.attributes.get("field2schemaname").getValue() != null) {
    			 control.addOption(options[3]);// optionset value for C
    		 control.addOption(options[4]);// optionset value for D
    		}
    		else if (Xrm.Page.data.entity.attributes.get("field3schemaname").getValue() != null) {
    			 control.addOption(options[5]);// optionset value for E
    		 control.addOption(options[6]);// optionset value for F
    		}
    		else // add all the option set value if all the 3 lookup are blank
    		{
    			 control.addOption(options[1]);
    			 control.addOption(options[2]);
    			 control.addOption(options[3]);
    			 control.addOption(options[4]);
    			 control.addOption(options[5]);
    			 control.addOption(options[6]);		 
    		}
    	}

    Hope this helps!!!

    If you find it helpful, Please mark as Verified.

    Best Regards,

    Shahbaaz

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
Victor Onyebuchi Profile Picture

Victor Onyebuchi 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans