Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Forums / Sales forum / Lookup field values fi...
Sales forum

Lookup field values filter depending upon another lookup field value

(0) ShareShare
ReportReport
Posted on by 619

I have a custom entity.  Added this custom entity as a subgrid in Contact form and I enabled quick create form for custom entity.  In custom entity, there are two lookup fields A and B.  B has reference of A. I would like to filter B values depending upon A value.  I have written javascript by using "addPreSearch" function while on load form.  It is working fine. 

But if A value is not there,  B should not show any values, I should empty the lookup values of B.  It is not working.  How can I empty the values of B if A value is not there.

Categories:
  • RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Lookup field values filter depending upon another lookup field value

    There are times where we have to educate end users/ client on how to do stuff. This may be one of that training you can provide to your users.

  • RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Lookup field values filter depending upon another lookup field value

    Then you can't hide that in  a support way. Sorry.

  • Saratha Profile Picture
    619 on at
    RE: Lookup field values filter depending upon another lookup field value

    Yes Ravi,  User have privilege to create the record.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Lookup field values filter depending upon another lookup field value

    Unfortunately no, you can't hide that new button. One option as suggested above is to remove the create privledges if that suits your requirement (which I doubt as its a contact entity which you may want your users to create)

    Hope this helps.

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,626 on at
    RE: Lookup field values filter depending upon another lookup field value

    Hi,

    if I am not mistaken, you cant hide the "New" button, but you can disable it by remove the create privilege from related security role.

  • Saratha Profile Picture
    619 on at
    RE: Lookup field values filter depending upon another lookup field value

    Hi Ravi,

    This logic is working fine. Thanks for your suggestion. If A is empty, B lookup values are empty.  But it showed to create new in lookup.  I made it as read only of all the field to restrict new creation.  Whether can we hide "New" option in lookup value selector.

  • Alex Fun Wei Jie Profile Picture
    33,626 on at
    RE: Lookup field values filter depending upon another lookup field value

    Hi,

    if you are refering to clearing the lookup B filter, refer to the suggestion from Ravi

  • Saratha Profile Picture
    619 on at
    RE: Lookup field values filter depending upon another lookup field value

    Hi Wei Jie,

    Thanks for your suggestion. I can able to set the field as null. But I would like to set the lookup values are empty.

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Lookup field values filter depending upon another lookup field value

    If you want to restrict on the front end i.e within the form then you need to add some dummy filter which doesn't return any results. Below is a sample script which filters contacts by account

    =========

    function filterContactsByAccount() {

       if (Xrm.Page.getControl("new_contact") !== null) {        

           Xrm.Page.getControl("new_contact").addPreSearch(addFilter);

       }

    }

    function addFilter() {

       var AccountId = Xrm.Page.data.entity.attributes.get("new_account").getValue();

       if (AccountId !== null) {

           AccountId = AccountId[0].id.replace("{", "").replace("}", "");

           var filter = "<filter type='and'>" +

               "<condition attribute='parentcustomerid' operator='eq' value='" + AccountId + "'/>" +

               "</filter>";

           Xrm.Page.getControl("new_contact").addCustomFilter(filter);

       }

       else {

           var filterDummy = "<filter type='and'>" +

               "<condition attribute='contactid' operator='null' />" +

               "</filter>";

           Xrm.Page.getControl("new_contact").addCustomFilter(filterDummy);

       }

    }

    ===============

    Hope this helps.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at
    RE: Lookup field values filter depending upon another lookup field value

    Hi,

    If you want to do it on a save then you can simply do it with a workflow and use clear to clear the values.

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

News and Announcements

Now Available: 2025 Release Wave 2

Quick Links

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard > Sales

Featured topics

Product updates

Dynamics 365 release plans