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

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Lookup Control PreSear...
Power Apps
Unanswered

Lookup Control PreSearch & addCustomFilter

(0) ShareShare
ReportReport
Posted on by 6

I am trying to pre filter a lookup control in a model driven app based on the selection of a option list control. I have written the below script to be executed on change evented of the option list control to trigger the filtering. I am not receiving any errors but for some reason it is not filtering the lookup.

 

var Sdk = window.Sdk || {};

 

// set 'Sdk.setServiceFilter' in the Case form onchange event handler region field

Sdk.setServiceFilter = function (executionContext) {

               

    // get the form context

    formContext = executionContext.getFormContext();

               

                //get region field value

    var region=formContext.getAttribute("cssp_region").getText();

                Xrm.Page.getAttribute("cssp_serviceregion").setValue(region);

               

                if (region != 'global') {

                                //Prepare Condition for the filter

                                Sdk.filterServices = function () {

 

                                                // Only show services where region is like selected region

                                                var ServicesFilter = "<filter type='and'><condition attribute='cssp_regiontext' operator='like' value='%" region "%' /></filter>";

                                                formContext.getControl("cssp_service").addCustomFilter(ServicesFilter, "cssp_service");

                                }

                               

                                //Apply filter to the lookup field

                                formContext.getControl("cssp_service").addPreSearch(Sdk.filterServices);

                }

}

Categories:
  • SThompson Profile Picture
    6 on at

    Ok I was able to adjust my script to the following and started with a new lookup field (i.e. removed the existing look up field I had and added a new one to the form)

     

    SetLookupField = function(executionContext) {

    formContext = executionContext.getFormContext();

    formContext.getControl("cssp_service").addPreSearch(FilterService);

    }

     

    FilterService = function() {

    var Region = formContext.getAttribute("cssp_region").getText();

    var ServiceFilter = "<filter type = 'and'><condition attribute = 'cssp_regiontext' operator = 'like' value = '%"+Region+"%'/></filter>"

    formContext.getControl("cssp_service").addCustomFilter(ServiceFilter);

     

    }

     

    Starting from scratch this script is working and starts filtering the data after I click “All records”. It does not filter the recently used items. I tried disabling the recently used items on the control, but then the filter is no longer properly working. It is like its not properly refreshing unless I navigate from the recently used items to the “All Records” view. Instead ite just not showing any items. Any ideas?

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

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 352 Super User 2026 Season 2

#2
M Bilal Khan Profile Picture

M Bilal Khan 323

#3
Mohsin Ali Profile Picture

Mohsin Ali 226

Last 30 days Overall leaderboard