Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Issue with passing argument to addPreSearch

Posted on by Microsoft Employee

Hello everyone!

I have create a small javascript to pre search lookup fields. Although it does work with hardcoded attributes , it won't work by passing arguments to the function. 

This is my "hardcoded" function :


function preFilterLookup() { Xrm.Page.getControl("new_attribute").addPreSearch(addLookupFilter); } function addLookupFilter() { var fetchXml = "<filter type='and'><condition attribute='new_attributeRelatedField' operator='eq' value='false' /></filter>"; Xrm.Page.getControl("new_attribute").addCustomFilter(fetchXml); }

I have tried to pass the argument by using this function but i get error:

function preFilterLookup(fieldname) {    


Xrm.Page.getControl(fieldname).addPreSearch(addLookupFilter(fieldname)); 	
     
}


function addLookupFilter(fieldname) {
    
       var fetchXml = "<filter type='and'><condition attribute='new_attributeRelatedField' operator='eq' value='false' /></filter>";            
		Xrm.Page.getControl(fieldname).addCustomFilter(fetchXml);
   
}


I think it has something to do with scopes of functions. I also tried to create a var in main function , and assign to it the fieldname parameter and then pass that variable into addLookupFilter but still didn't work.

Any help?


*This post is locked for comments

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Issue with passing argument to addPreSearch

    Thank you both for your answers! It worked

  • Verified answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Issue with passing argument to addPreSearch

    Use this code:

    function preFilterLookup(fieldname) {    

    Xrm.Page.getControl(fieldname).addPreSearch(function() {

    addLookupFilter(fieldname);

    });  

    }

    function addLookupFilter(fieldname) {

          var fetchXml = "<filter type='and'><condition attribute='new_attributeRelatedField' operator='eq' value='false' /></filter>";            

    Xrm.Page.getControl(fieldname).addCustomFilter(fetchXml);

    }

  • Verified answer
    a33ik Profile Picture
    a33ik 84,323 Most Valuable Professional on at
    RE: Issue with passing argument to addPreSearch

    Try to replace line

    Xrm.Page.getControl(fieldname).addPreSearch(addLookupFilter(fieldname));

    with something similar to following:

    Xrm.Page.getControl(fieldname).addPreSearch(function(){
    
    addLookupFilter(fieldname);
    
    });


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!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans