Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Need help why this Js code is not working

Posted on by 815

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

  • Syed_Faisal Profile Picture
    Syed_Faisal 815 on at
    RE: Need help why this Js code is not working

    Thanks gowtham for the code !

  • Verified answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Need help why this Js code is not working

    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 
    
            }      
    
        }
  • RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Need help why this Js code is not working

    Hi Syed,

    Could you please share some mroe details on what exactly you are looking for? Screenshot may be useful.

  • Syed_Faisal Profile Picture
    Syed_Faisal 815 on at
    RE: Need help why this Js code is not working

    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,

  • Suggested answer
    gdas Profile Picture
    gdas 50,085 on at
    RE: Need help why this Js code is not working

    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".

  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Need help why this Js code is not working

    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.

  • Syed_Faisal Profile Picture
    Syed_Faisal 815 on at
    RE: Need help why this Js code is not working

    Hi Alex,

    i tried the previous code without space but no luck. Just thought to give you update.

  • Syed_Faisal Profile Picture
    Syed_Faisal 815 on at
    RE: Need help why this Js code is not working

    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)

  • Syed_Faisal Profile Picture
    Syed_Faisal 815 on at
    RE: Need help why this Js code is not working

    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 .

  • Suggested answer
    Alex Fun Wei Jie Profile Picture
    Alex Fun Wei Jie 33,626 on at
    RE: Need help why this Js code is not working

    Hi,

    you also can try as below.

    I notices there is a space.

    operator='eq' value='" + accountId + "' />" +  change into

    operator='eq' value='"+ accountId +"' />"+

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,240 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans