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 :
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:
I have the same question (0)
  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

    in your JS, put a checking if lookup A is null, then set the lookup B null. You can use below JS code to set value null, if you are using v9.0 , remember use formcontext.

    Xrm.Page.getAttribute(lookupFieldName).setValue(null);

    V9.0

    formContext.getAttribute("lookupFieldName").setValue(null);

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    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.

  • Verified answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    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.

  • Saratha Profile Picture
    619 on at

    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.

  • Alex Fun Wei Jie Profile Picture
    33,628 on at

    Hi,

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

  • Saratha Profile Picture
    619 on at

    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.

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,628 on at

    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.

  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    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.

  • Saratha Profile Picture
    619 on at

    Yes Ravi,  User have privilege to create the record.

  • RaviKashyap Profile Picture
    55,410 Moderator on at

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

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

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

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans