Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

option set value if lookup field contain data

Posted on by Microsoft Employee

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
    gdas 50,085 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
    Shahbaaz Ansari 6,203 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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans