Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Filter Contact Lookup associated with accounts

(0) ShareShare
ReportReport
Posted on by

Hi experts,

In the case record:

I have a customer lookup field from which the user can select the account.

I also have a contact lookup field and I have applied a filter in field properties to only show contacts related to the selected account.

Screen-Shot-2018_2D00_03_2D00_27-at-2.01.48-PM.png

Now, this filter works fine in the main form:

Screen-Shot-2018_2D00_03_2D00_27-at-1.59.36-PM.png

However, this filter is not being applied in the process stage of the form:
Screen-Shot-2018_2D00_03_2D00_27-at-1.57.16-PM.png

How can I make the filter work in both areas? 

Thanks,

Jon

*This post is locked for comments

  • Alex Fun Wei Jie Profile Picture
    33,626 on at
    RE: Filter Contact Lookup associated with accounts

    Hi,

    could be someone override your code.

  • Verified answer
    Alex Fun Wei Jie Profile Picture
    33,626 on at
    RE: Filter Contact Lookup associated with accounts

    Hi,

    Replace the correct schema name in below highlighted part. 

    function filterLookup() 
    {
    //Check if the control exist on the form
    if (Xrm.Page.getControl("header_process_new_contact") != null) 
    {
    // add the event handler for PreSearch Event
    Xrm.Page.getControl("header_process_new_contact").addPreSearch(addFilter);
    }

    }

    function addFilter() 
    {
    var accountId = null;
    var accountLookup;
    var fetchQuery;
    try 
    {
    //Check if control exist on form
    if (Xrm.Page.getControl("header_process_customer") != null && Xrm.Page.getControl("header_process_customer").getAttribute().getValue() != null) 
    {
    //Get Account lookup value
    accountLookup = Xrm.Page.getControl("header_process_customer").getAttribute().getValue();
    //Get the account id
    accountId = accountLookup[0].id;
    }

    //Build fetch
    if (accountId != null || accountId != undefined) 
    {
    fetchQuery = "<filter type='and'>" +
    "<condition attribute=' statecode' operator='eq' value='0' />" + 
    "<condition attribute='customerid' operator='eq' value='" + accountId + "' / >" +
    "</filter>";

    fetchQuery = "<filter type='and'>" 
    + "<condition attribute='statecode' operator='eq' value='0' />" 
    + "<condition attribute='customerid' operator='eq' value='" + accountId + "' />" + "</filter>";
    Xrm.Page.getControl("header_process_new_contact").addCustomFilter(fetchQuery);

    Xrm.Page.getControl("header_process_new_contact").addCustomFilter(fetchQuery);
    }

    catch (e) 
    {
    Xrm.Utility.alertDialog("addFilter Error: " + (e.description || e.message));
    }

    }

  • Community Member Profile Picture
    on at
    RE: Filter Contact Lookup associated with accounts

    Hi Wei Jie,

    The account schema in contact is "parentcustomerid." I have changed it and it has started working again.

    I still don't understand what caused it to change/why it stopped working.

    Thanks for your prompt help as always!

    Jon

  • Verified answer
    Alex Fun Wei Jie Profile Picture
    33,626 on at
    RE: Filter Contact Lookup associated with accounts

    Hi Jon

    'Contact' entity doesn't contain attribute with Name = 'customerid'

    can you check the account schema name in contact again?

    in my environment, the account schema name is "parentcustomerid" .

    1072.12.png

  • Community Member Profile Picture
    on at
    RE: Filter Contact Lookup associated with accounts
    Hi Wei Jie,

    Unhandled Exception: System.ServiceModel.FaultException`1[[Microsoft.Xrm.Sdk.OrganizationServiceFault, Microsoft.Xrm.Sdk, Version=9.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35]]: 'Contact' entity doesn't contain attribute with Name = 'customerid' and NameMapping = 'Logical'. MetadataCacheDetails: ProviderType=Dynamic, StandardCache=True, IsLoadedInStagedContext = False, Timestamp=1851154, MinActiveRowVersion=1851154Detail: <OrganizationServiceFault xmlns:i="www.w3.org/.../XMLSchema-instance" xmlns="schemas.microsoft.com/.../Contracts"> <ActivityId>367433ef-1ac9-4480-81ac-3d128d74afcc</ActivityId> <ErrorCode>-2147217149</ErrorCode> <ErrorDetails xmlns:d2p1="schemas.datacontract.org/.../System.Collections.Generic" /> <Message>'Contact' entity doesn't contain attribute with Name = 'customerid' and NameMapping = 'Logical'. MetadataCacheDetails: ProviderType=Dynamic, StandardCache=True, IsLoadedInStagedContext = False, Timestamp=1851154, MinActiveRowVersion=1851154</Message> <Timestamp>2018-04-04T08:53:43.9024848Z</Timestamp> <ExceptionRetriable>false</ExceptionRetriable> <ExceptionSource i:nil="true" /> <InnerFault i:nil="true" /> <OriginalException i:nil="true" /> <TraceText i:nil="true" /> </OrganizationServiceFault>

    Thanks,

    Jon

  • Alex Fun Wei Jie Profile Picture
    33,626 on at
    RE: Filter Contact Lookup associated with accounts

    Hi Jon,

    it stated specified field does not exist, can you download the log file and upload here ?

  • Community Member Profile Picture
    on at
    RE: Filter Contact Lookup associated with accounts

    Hi guys,

    I don't understand why but suddenly the filtering stopped working on the case record..

    Screen-Shot-2018_2D00_04_2D00_04-at-2.08.42-PM.png

    Screen-Shot-2018_2D00_04_2D00_04-at-2.08.42-PM.png

    Screen-Shot-2018_2D00_04_2D00_04-at-2.08.12-PM.png

    Thanks,

    Jon

  • Alex Fun Wei Jie Profile Picture
    33,626 on at
    RE: Filter Contact Lookup associated with accounts

    Hi Jon,

    glad it worked. You're welcome.

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    33,626 on at
    RE: Filter Contact Lookup associated with accounts

    Hi Jon,

    by looking at the css

    for contact field , use "header_process_new_contact"

    for account field, can you get another screen shot for that?

  • Community Member Profile Picture
    on at
    RE: Filter Contact Lookup associated with accounts

    Hi Shahbaaz,

    That worked.

    Thank you so much Shahbaaz and Wei Jie for helping me make it work. Appreciate it!!!

    Thanks again!

    Jon

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

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Kudos to the April Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard > Microsoft Dynamics CRM (Archived)

#1
Mohamed Amine Mahmoudi Profile Picture

Mohamed Amine Mahmoudi 83 Super User 2025 Season 1

#2
Community Member Profile Picture

Community Member 52

#3
dkrishna Profile Picture

dkrishna 6

Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans