Question Status

Verified
darklord asked a question on 18 Mar 2015 6:57 AM

Hi all,
I have to filter my lookup field on the form based on some criteria. I have achieved the desired result by using the combination of addPreSearch and addCustomFilter.


Unfortunately when the filter returns no record, the drop down on the lookup shows 5 suggestions. When you click on add more records the dialog box correctly displays no record.

My question 

Is there any way to stop the drop down on the look up field from displaying suggestions and show an empty drop down(or nothing at all) ?

For your reference this is the syntax I am using

function preFilterLookup() {
    
    Xrm.Page.getControl("customerid").addPreSearch(function () {
        addLookupFilterOnGlobalCustomerName();

    });
}

According to you guys is there a workaround to the same?

Here are the images, first image is where the problem lies, the filter returns nothing as displayed in the second image.

IMAGE 1

IMAGE 2

Thanks in advance for any help.

Reply
Aileen Gusni responded on 18 Mar 2015 8:03 AM

Hi Darklord,

Actually If you use the presearch filter like that it should show exactly the same between the first and the lookup more records in term of Data.

So, in fact your data should return no or should return 5? Just checking first to know the issue.

Thanks.

Reply
darklord responded on 18 Mar 2015 8:36 AM

Hi Aileen,

Thanks for taking an interest, much appreciated

Problem is that it is not doing so.

The first window(drop down) shows 5 records(which is incorrect)

The second window(look for more rec) shows no records(which is correct)

And it is the same for each and every filter that I have used

example: (Entity) account--> name starts with Z%

result should be no record but the first window shows five and the second window none

As soon as the filter starts returning some value the problem is fixed.

I hope I am making some sense

Reply
Aileen Gusni responded on 18 Mar 2015 8:37 AM

Darklord,

I see..

Can you share the javascript code?

Thanks.

Reply
darklord responded on 18 Mar 2015 8:43 AM
preFilterLookup() is called on form load
function preFilterLookup() {
    
    Xrm.Page.getControl("customerid").addPreSearch(function () {
        addLookupFilterOnGlobalCustomerName();});
}
function addLookupFilterOnGlobalCustomerName() { var fetchXml = '<filter type="and"><condition attribute="uc_salesmanager" operator="eq" uiname="' + Xrm.Page.context.getUserName() + '" uitype="systemuser" value="' + Xrm.Page.context.getUserId() + '" /><filter type="or"><condition attribute="uc_status_xcc" operator="eq" value="994830001" /><condition attribute="uc_status_xcc" operator="eq" value="994830000" /></filter></filter>';
Xrm.Page.getControl("customerid").addCustomFilter(fetchXml, "account"); }

Reply
Verified Answer
Aileen Gusni responded on 18 Mar 2015 9:26 AM

Hi Dark Lord

It seems the first one display Contact results so that you got records displayed.

While in your code, you filter only Account..

Xrm.Page.getControl("customerid").addCustomFilter(fetchXml, "account");

Customer Id is a special field that can be refer to contact or account in one field.

The filter you used in the code only applicable to filter Account, meanwhile Customer ID can also retrieve Contact, which is for Contact you don't apply any filter.

mscrmshop.blogspot.com/.../addcustomfilter-method-for-lookup.html

See this:

"Things to remember

addCustomFilter method takes two parameters ‘filter’ and the ‘entityLogicalName’. The entityLogicalName is optional and if this parameter is provided, the filter will only apply to that entity type. Otherwise it will apply to all types of entities returned.

For e.g. customer lookup control display account and contact records. If we don’t provide entityLogicalName  parameter, the filter will apply to both account and contact records and if we provide “account” as a  parameter then filter will only be applied to account records not to contact records."

Try to remove the "account".

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

Just worry that will give error because contact doesn't have those fields in condition, but let's try first.

Hope this time can work.

Thanks.

Reply
darklord responded on 18 Mar 2015 10:02 PM

Aileen u are a genius. Thanks a million for the solution.

What I did was to add another fetch query to the same control, this one was for contact, in this query I intentionally gave a criteria which cannot be true ever. Thus contact will return an empty recordset always.

But the fetch on account may or may not return some records (which I want)

Thank you again for helping me out and may God grant you all your wishes.

Reply
Verified Answer
Aileen Gusni responded on 18 Mar 2015 9:26 AM

Hi Dark Lord

It seems the first one display Contact results so that you got records displayed.

While in your code, you filter only Account..

Xrm.Page.getControl("customerid").addCustomFilter(fetchXml, "account");

Customer Id is a special field that can be refer to contact or account in one field.

The filter you used in the code only applicable to filter Account, meanwhile Customer ID can also retrieve Contact, which is for Contact you don't apply any filter.

mscrmshop.blogspot.com/.../addcustomfilter-method-for-lookup.html

See this:

"Things to remember

addCustomFilter method takes two parameters ‘filter’ and the ‘entityLogicalName’. The entityLogicalName is optional and if this parameter is provided, the filter will only apply to that entity type. Otherwise it will apply to all types of entities returned.

For e.g. customer lookup control display account and contact records. If we don’t provide entityLogicalName  parameter, the filter will apply to both account and contact records and if we provide “account” as a  parameter then filter will only be applied to account records not to contact records."

Try to remove the "account".

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

Just worry that will give error because contact doesn't have those fields in condition, but let's try first.

Hope this time can work.

Thanks.

Reply