web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

No record found.

News and Announcements icon
Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

addPreSearch not working in dynamics crm 365 9.0

(0) ShareShare
ReportReport
Posted on by 737

Hi experts,

I am filtering district lookup based on a hardcoded district name value but it is not working  

on form load javascript code is like this

var fltr = "<filter type='and'><condition attribute='pcl_name' value='udaipur' operator='eq'/></filter>";
Xrm.Page.getControl("new_district").addPreSearch(AddFilter(fltr ));

function AddFilter(filterStr)

{
        Xrm.Page.getControl("new_district").addCustomFilter(filterStr);
}

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    If you want to show hardocoded values then you don't need to use code for this. You can simply create a view with your values and under the Lookup properties (form editor), select that view.

    If you want to do this using the code, you could refer teh below sample code. The method needs to be called on load.

    ==============

    export function FilteredRecords(): void {

       Xrm.Page.getControl("new_district").addPreSearch(function () {

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

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

           });

    }

    ================

    Also, ensure that you udaipur as the correct value.

    Hope this helps.

  • Suggested answer
    Dynamics365 Rocker Profile Picture
    7,755 on at

    Why are you not using OOB lookup filter?

    Below link will explain you to use filter lookup OOB:

    community.dynamics.com/.../224515

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi,

    Try with this-

    //Register below method in onload event

    function onloadAddress()

    {

    Xrm.Page.getControl("new_district").addPreSearch(AddFilter);

    }

    function AddFilter()

    {

    var fltr = "<filter type='and'><condition attribute='pcl_name' value='udaipur' operator='eq'/></filter>";

           Xrm.Page.getControl("new_district").addCustomFilter(fltr);

    }

  • B K Sharma Profile Picture
    737 on at

    Dear Ravi Thanks for ur prompt reply

    if we write all the code in the addPreSearch method everything works fine for the hardcoded value but when i pass this filter value from run time query result it did't work, even i am unable to debug the code written in addPreSearch method

    Xrm.Page.getControl("wc_distirct").addPreSearch(function () {

              var userDist = getUserDistricts(Xrm.Page.context.getUserId());

             if(userDist.length > 0)

               {

                   var distcount = 0;

                   var filterStr = "<filter type='and'><filter type='or'>";

                   for (distcount = 0; distcount < userDist.length; distcount++)

                   {

                       filterStr += "<condition attribute='pcl_name' operator='eq' value='"+ userDist[0].attributes["pcl_name"].value +"'/>";

                   }

                   filterStr += "</filter></filter>";

                   Xrm.Page.getControl("wc_distirct").addCustomFilter(filterStr);

                    }

             });

    function getUserDistricts(UserId)

    {  

       var fetchDis = " <fetch distinct='true' mapping='logical' output-format='xml-platform' version='1.0'> " +

                      " <entity name='pcl_district'> " +

                      "    <attribute name='pcl_name'/> <attribute name='pcl_districtid'/> " +

                      "    <order descending='false' attribute='pcl_name'/> " +

                      "    <link-entity name='pcl_pcl_district_systemuser' intersect='true' visible='false' to='pcl_districtid' from='pcl_districtid'> " +

                      "        <link-entity name='systemuser' to='systemuserid' from='systemuserid' alias='ad'> " +

                      "            <filter type='and'>  " +

                      "                <condition attribute='systemuserid' value='" + UserId + "' operator='eq'/>  " +

                      "            </filter>  " +

                      "    </link-entity>  " +

                      " </link-entity>  " +

                      " </entity>  " +

                      " </fetch>";

       var userdDist = XrmServiceToolkit.Soap.Fetch(fetchDis);

       return userdDist;

    }

    If i am wrong anywhere than pls guid

  • Verified answer
    B K Sharma Profile Picture
    737 on at

    now this code is working

    var userDist = getUserDistricts(Xrm.Page.context.getUserId());

               if(userDist.length > 0)

               {

                   var distcount = 0;

                   var filterStr = "<filter type='and'><filter type='or'>";

                   for (distcount = 0; distcount < userDist.length; distcount++)

                   {

                       filterStr += "<condition attribute='pcl_name' value='"+ userDist[distcount].attributes["pcl_name"].value +"' operator='eq'/>";

                   }

                   filterStr += "</filter></filter>";

                   Xrm.Page.getControl("wc_distirct").addPreSearch(function () {Xrm.Page.getControl("wc_distirct").addCustomFilter(filterStr);});              

               }  

    Means you can not write code in addPreSearch method and you can use outside variable in this method

  • RaviKashyap Profile Picture
    55,410 Moderator on at

    Hi,

    Yes, thats why I asked if you want to show hardocoded values or not? If you want a dynamics values then you first need to get the values before adding it to the event. The sample I shared was dynamics, I changed it to static.

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Stars!

Meet the Microsoft Dynamics 365 Contact Center Champions

We are thrilled to have these Champions in our Community!

Congratulations to the April Top 10 Community Leaders

These are the community rock stars!

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans