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 :
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

I have the same question (0)
  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hi,

    For BPF you need to write code, this post should help you to implement your requirements: blogs.webrange.com.au/.../how-to-filter-a-lookup-field-in-business-process-flow-bpf-in-dynamics-crm

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Did you filter using javascript?? if yes then for lookup which is in BPF has the different schema name

    "header_process_fieldschemaname".

    Thanks,

    Shahbaaz

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Did you filter using javascript?? if yes then for lookup which is in BPF has the different schema name

    "header_process_fieldschemaname".

    You can also refer this link,

    www.inogic.com/.../how-to-apply-script-on-header-fields-and-bpf-fields

    Thanks,

    Shahbaaz

  • Community Member Profile Picture
    on at

    Hi Shahbaaz,

    Thanks for the link.

    I used the following code to filter contact based on account but I'm getting filterLookup function not defined at eval script error. Please help.

    function filterLookup() 
    {
    	//Check if the control exist on the form
    	if (Xrm.Page.getControl(“header_process_new_contactid”) != null) 
    	{
    	// add the event handler for PreSearch Event
    	Xrm.Page.getControl(“header_process_new_contactid”).addPreSearch(addFilter);
    	}
    
    }
    
    function addFilter() 
    {
    	var accountId = null;
    	var accountLookup;
    	var fetchQuery;
    	try 
    	{
    		//Check if control exist on form
    		if (Xrm.Page.getControl(“header_process_customerid”) != null && Xrm.Page.getControl(“header_process_customerid”).getAttribute().getValue() != null) 
    		{
    			//Get Account lookup value
    			accountLookup = Xrm.Page.getControl(“header_process_customerid”).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>”;
    			Xrm.Page.getControl(“header_process_new_contactid”).addCustomFilter(fetchQuery);
    		}
    	} 
    	catch (e) 
    	{
    		Xrm.Utility.alertDialog(“addFilter Error: ” + (e.description || e.message));
    	}
    
    }


    Thanks,

    Jon

  • Suggested answer
    Shahbaaz Ansari Profile Picture
    6,211 on at

    Hi Jon,

    Now try below code, it was due to "" quote was not proper

    ******************************************************

    function filterLookup()

    {

    //Check if the control exist on the form

    if (Xrm.Page.getControl("header_process_new_contactid") != null)

    {

    // add the event handler for PreSearch Event

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

    }

    }

    function addFilter()

    {

    var accountId = null;

    var accountLookup;

    var fetchQuery;

    try

    {

    //Check if control exist on form

    if (Xrm.Page.getControl("header_process_customerid") != null && Xrm.Page.getControl("header_process_customerid").getAttribute().getValue() != null)

    {

    //Get Account lookup value

    accountLookup = Xrm.Page.getControl("header_process_customerid").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>";

    Xrm.Page.getControl("header_process_new_contactid").addCustomFilter(fetchQuery);

    }

    }

    catch (e)

    {

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

    }

    }

    ******************************************************

    Thanks,

    Shahbaaz

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

    Hi,

    i copied your code and paste it to JS file, from the below picture you can see there are error,  try to change the " and ' again.

    58187.2.png

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

    }

    function addFilter()
    {
    var accountId = null;
    var accountLookup;
    var fetchQuery;
    try
    {
    //Check if control exist on form
    if (Xrm.Page.getControl("header_process_customerid") != null && Xrm.Page.getControl("header_process_customerid").getAttribute().getValue() != null)
    {
    //Get Account lookup value
    accountLookup = Xrm.Page.getControl("header_process_customerid").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>";
    Xrm.Page.getControl("header_process_new_contactid").addCustomFilter(fetchQuery);
    }
    }
    catch (e)
    {
    Xrm.Utility.alertDialog("addFilter Error: " + (e.description || e.message));
    }

    }// JavaScript source code

  • Community Member Profile Picture
    on at

    Hi Shahbaaz,

    Thank you for your reply. Your correction removed the error but the code is not filtering the lookup field.

    Thanks,

    Jon

  • Community Member Profile Picture
    on at

    Hi Wei Jie,

    Thank you for your reply. Your correction also removed the error but the code is not filtering the lookup field.

    Thanks,

    Jon

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

    Hi Jon,

    can you try to search the real schema name using below method ?

    Click F12, and point to the field. Do it for both fields.

    5344.5.png

    5344.5.png

    by passing the correct schema name, it worked.

    4834.4.png

  • Shahbaaz Ansari Profile Picture
    6,211 on at

    @Fun Wei Jei,

    can u share your code or schema name that you copied from Html for contact?

    you are using trial environment right?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans