Hi,
I got these below code help from the community friend, unfortunately now it is not working. Can you pl help.
function filterLookup() {
//Check if the control exist on the form
if (Xrm.Page.getControl("header_process_primarycontactid") != null) {
// add the event handler for PreSearch Event
Xrm.Page.getControl("header_process_primarycontactid").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='parentcustomerid' operator='eq' value='" + accountId + "' />" +
"</filter>";
//add custom filter
Xrm.Page.getControl("header_process_primarycontactid").addCustomFilter(fetchQuery);
}
} catch (e) {
Xrm.Utility.alertDialog("addFilter Error: " + (e.description || e.message));
}
}
-----------------------------------------------------------------------------
function filterLookup() {
//Check if the control exist on the form
if (Xrm.Page.getControl("header_process_new_address") != null) {
// add the event handler for PreSearch Event
Xrm.Page.getControl("header_process_new_address").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='new_customername' operator='eq' value='" + accountId + "' />" +
"</filter>";
//add custom filter
Xrm.Page.getControl("header_process_new_address").addCustomFilter(fetchQuery);
}
} catch (e) {
Xrm.Utility.alertDialog("addFilter Error: " + (e.description || e.message));
}
}
*This post is locked for comments
Thanks gowtham for the code !
Hi Sayed ,
You can add/Remove filter from the lookup based on some condition . Please check below code .
function filterContactLookup() { var showAllrecords = true; // Get the value true /false based on your condition if (Xrm.Page.getControl("primarycontactid") != null) { if (showAllrecords == false) Xrm.Page.getControl("primarycontactid").addPreSearch(addContactFilter);// To add Filter else Xrm.Page.getControl("new_casetype").removePreSearch(addFilter); // To remove filter } }
Hi Syed,
Could you please share some mroe details on what exactly you are looking for? Screenshot may be useful.
Hi Ravi & Gowtham,
Thanks for the suggestion, actually on Onchange event i have not updated latest function name which gowtham advised to do.
I have copied the same code for the Contact Lookup and Address Lookup. Both code are absolutely working fine.
I just need 1 clarification here if we can do within the code, Question is on the address filter, it filter based on customer as we forcing by JS. We also need option for to uncheck filter to show all other addresses. I m unable to find that option here for to UnCheck filter to find more records.
Thanks again for the help,
Hi Sayed ,
As I said I have rename function with different name instead of "filterLookup " I gave the function name "filterContactLookup" . So you have to replace in the onload or onchange event where you take the old function name (filterLookup ) reference in the form properties with new name "filterContactLookup".
Hi Syed,
The error "ReferenceError: filterLookup is not defined" means CRM is unable to find this nethid ehich could be because of caching so publish customization snd try in new browser.
This can also hapoen if you have any syntax error in your code. You can check Dev tools (F12) to see if you gettubg any error.
Hope this helps.
Hi Alex,
i tried the previous code without space but no luck. Just thought to give you update.
Hi Gautam,
The code which you have sent for contactlookup is working fine after filter lookup, getting this below error.
ReferenceError: filterLookup is not defined
at eval (eval at RunHandlerInternal (crm/.../ClientApiWrapper.aspx), <anonymous>:1:1)
at RunHandlerInternal (crm/.../ClientApiWrapper.aspx)
at RunHandlers (crm/.../ClientApiWrapper.aspx)
at ExecuteHandler (crm/.../ClientApiWrapper.aspx)
at Mscrm.TurboForm.Control.CustomScriptsManager.$Cb_1 (crm/.../formcontrols.js)
at Mscrm.TurboForm.Control.CustomScriptsManager.executeHandler (crm/.../formcontrols.js)
at Mscrm.TurboForm.Control.CustomScriptsManager.executeHandlerByDescriptor (crm/.../formcontrols.js)
at crm/.../formcontrols.js
at crm/.../global.ashx
at Mscrm.TurboForm.Control.Data.LookupDataAttribute.fireOnChange (crm/.../formcontrols.js)
Thank You all for the kind help.
Mentioned code is define on Case module and it was used previously on lead page and i used the same code from the lead page and modified it.
Let me run gautam code and see how it works .
Hi,
you also can try as below.
I notices there is a space.
operator='eq' value='" + accountId + "' />" + change into
operator='eq' value='"+ accountId +"' />"+
Stay up to date on forum activity by subscribing. You can also customize your in-app and email Notification settings across all subscriptions.
André Arnaud de Cal... 291,240 Super User 2024 Season 2
Martin Dráb 230,149 Most Valuable Professional
nmaenpaa 101,156