Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Limit lookup values based on a optionset

Posted on by Microsoft Employee

Hi all,

I'd like to know if is possible to limit the values that come from a lookup field, depending on the values that come from an optionset field I have in the same form.

thank you in advance.

*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Limit lookup values based on a optionset

    Fedor, lots of thanks, that could really help me, I'll post down my code here, for  it may help someone in the future:

    function preFilterLookup() {

    Xrm.Page.getControl("new_fasedopipelineid").addPreSearch(function () {

    addLookupFilter();

      });

    }

    function addLookupFilter() {

       var tipoOportunidade = Xrm.Page.getAttribute("new_tipodeoportunidade").getText();

       if (tipoOportunidade == "Run Rate") {

    Xrm.Page.ui.controls.get("new_subfasedopipelineid").setVisible(false);

           fetchXml = "<filter type='and'><condition attribute='statuscode' operator='eq' value='100000000' /></filter>";

    Xrm.Page.getControl("new_fasedopipelineid").addCustomFilter(fetchXml);

       }

    else{

    Xrm.Page.ui.controls.get("new_subfasedopipelineid").setVisible(true);

    fetchXml = "<filter type='and'><condition attribute='statuscode' operator='ne' value='2' /></filter>";

    Xrm.Page.getControl("new_fasedopipelineid").addCustomFilter(fetchXml);

    }

    }

    I used the statuscode field in the lookup I wanted to filter, so I was able in the "if" to bring what I wanted to filter, and in the "else" everything = ).

  • Aiden Kaskela Profile Picture
    Aiden Kaskela 19,692 on at
    RE: Limit lookup values based on a optionset

    Hi Kyle,

    That's awesome information - I'm definitely bookmarking this post. Thanks!

    Aiden

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Limit lookup values based on a optionset

    Hi JoaoL,

    You can make your option set field global and add it on entity which you want to filter. And then use addCustomFilter() method to add a filter to this lookup.

    Something like this. It's the best supported solution, i think.

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Limit lookup values based on a optionset

    You can use the methods support by the Lookup control type to filter the lookup field with your own fetchXML filter.  Try the addCustomFilter() method, passing your optionset value as the criteria for a fetchXML filter.  Note that you have to use the addCustomFilter() method in conjunction with the addPreSearch() method, as explained in the SDK.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Limit lookup values based on a optionset

    = ,(

    sad world.

    that's ok, thanks for the help ^^.

  • Suggested answer
    Aiden Kaskela Profile Picture
    Aiden Kaskela 19,692 on at
    RE: Limit lookup values based on a optionset

    Joaol,

    My apologies, I misread your question and thought you wanted to do it in a grid. You may be able to take the same approach since the lookup uses a view but I haven't tried that.

    There is an option for Related Record Filtering but that only works for other entities. Unfortunately I don't think there's a way to do it.

    Aiden

  • Suggested answer
    Aiden Kaskela Profile Picture
    Aiden Kaskela 19,692 on at
    RE: Limit lookup values based on a optionset

    Hi JoaoL,

    You can do this, but it's not strictly supported. You can dynamically set the FetchXml of your grid, so as long as you can build a query based on the option chosen, you could do it.

    Here's a good blog post about how to set FetchXml on your grid:

    joshimandarblog.wordpress.com/.../crm-2015-applying-custom-fetchxml-to-a-subgrid-using-javascript

    You'd want to run javascript OnLoad of your form and OnChange of the option set, which will build the FetchXml query and set it on the grid.

    If this helps I'd really appreciate if you would mark this as a Verified answer.

    Thanks,

     Aiden

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