web
You’re offline. This is a read only version of the page.
close
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

I have the same question (0)
  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    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

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    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);
                    }
    
                });
            }


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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans