Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Filter optionset values based on lookup using js.

Posted on by 240

Hi,

Can anyone help me regarding, how to filter optionset values using lookup.

Ex: If lookupText is A optionsetvales x,y,z

if lookupText is B opetionsetvalues a,b,c

*This post is locked for comments

  • Priya1 Profile Picture
    Priya1 30 on at
    RE: Filter optionset values based on lookup using js.

    Hi,

    I also got the same requirement in which I need to filter based upon lookup entity value so first I am rederiving stage value from the lookup entity and then setting in the lookup  and below code is working fine for me:

                          selectedStage = result["mcshhs_stage"];

                           selectedStageValue = result["mcshhs_stage@OData.Community.Display.V1.FormattedValue"];

                           var optionset = context.ui.controls.getByName("mcshhs_stage");

                           var optionsetValues = optionset.getOptions();

                           optionset.clearOptions();

                           if (selectedStage != undefined && selectedStage != null && selectedStage != '') {

                               var stageArr = selectedStage.split(',');

                               var stagevalueArr = selectedStageValue.split(';');

                                   for (var i = 0; i < stageArr.length; i++) {

                                       var option = { text: stagevalueArr[i], value: stageArr[i] };

                                       optionset.addOption(option);

                                   }

  • Suggested answer
    Kokulan Profile Picture
    Kokulan 18,048 on at
    RE: Filter optionset values based on lookup using js.

    Hi

    Normally to achieve what you want in a well-supported way, is to use two entities and have two lookups on the form and filter them based on the selection of the  A.

    However, due to increasing demand for Cascaded OptionSets, there is in a way a kind of supported and recommended solution that you could use it in any entity form.

    Please have a look at these links

    [View:https://docs.microsoft.com/en-us/previous-versions/dynamicscrm-2016/developers-guide/gg594433(v=crm.8):750:50]

    [View:https://code.msdn.microsoft.com/create-dependent-844db2d1:750:50]

    Hope this helps

  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Filter optionset values based on lookup using js.

    Hi Miss Dynamics,

    How did you go with this? If you have got your answer, please close the thread by marking the helpful suggestion.

    Marking the suggestion as Verified helped other facing the same/similar issue. Appreciate if you can close your previous threads as well.

  • Suggested answer
    Sreevalli Profile Picture
    Sreevalli 3,256 on at
    RE: Filter optionset values based on lookup using js.

    Hi,

    If possible change your second field to Lookup (create new one). so that it would be OOB feature to filter value with in. see the link for quick help - community.dynamics.com/.../how-to-filter-lookups-by-related-records-in-dynamics-365

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Filter optionset values based on lookup using js.

    Hi ,

    Try with this  -

    function OptionSetAddRemoveBasedOnLookup() {
    
        var lookupItem = Xrm.Page.getAttribute("new_ContactLKUP").getValue(); // Replace lookup field Name
    
        if (lookupItem != null) {
            contactId = lookupItem[0].id;
            contactName =lookupItem[0].name;
          
            var optionSet = Xrm.Page.ui.controls.get("new_myoptionset");
            var optionSetValues = optionSet.getAttribute().getOptions(); // retrieve All options of Option set field
            optionSet.clearOptions();
            optionSetValues.forEach(function (element) {
                if(contactName == "Some Name")
                {
                    if (element.value == "831260009")  // Add Options which you want to show based on value , you can also check with Optionset text like element.text
                        optionSet.addOption(element);
                    if (element.value == "831260008")  // Remove options which you want to hide based on value 
                        optionSet.removeOption(element);
                }
                else if (contactName == "Diff Name")
                {
                    // You can also do for different lookup name
                }
    
            });
    
        }
    }
    


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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans