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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Filter lookup field us...
Power Apps
Answered

Filter lookup field using addCustomFilter control with multiple conditions

(0) ShareShare
ReportReport
Posted on by 30

Hello everyone.

 

I have two main entities - Project and Plan.

 

First user needs to add a new project in a Project entity with fields: country (lookup field to country entity), type (lookup field to type entity) and projectname (text field)

 

Then user creates a plan in a Plan entity with fields: country (lookup field to country entity), type (lookup field to type entity), projectname (lookup field to Project entity), planname (text field) 

 

What I need to do: I need to use country field and type field in Plan entity to work as a filter for projectname lookup field in Plan entity. So once user choose country and type then he sees only related projectnames with given country and type.

 

I have written a javascript code: 

SetLookupField = function(executionContext) {
formContext = executionContext.getFormContext();
formContext.getControl("project").addPreSearch(FilterProject);
}

FilterProject = function() {
var Country = formContext.getAttribute("country").getValue[0].id;
var ProjectFilter = "<filter type = 'and'>
<condition attribute = 'country' operator = 'eq' value = '"+Country+"'/>
</filter>;
formContext.getControl("project").addCustomFilter(ProjectFilter);

}

 

and it works fine, it filteres my project by country. but when I add another condition for type:

 

SetLookupField = function(executionContext) {
formContext = executionContext.getFormContext();
formContext.getControl("project").addPreSearch(FilterProject);
}

FilterProject = function() {
var Country = formContext.getAttribute("country").getValue[0].id;
var Type = formContext.getAttribute("type").getValue[0].id;
var ProjectFilter = "<filter type = 'and'>
<condition attribute = 'country' operator = 'eq' value = '"+Country+"'/>
<condition attribute = 'type' operator = 'eq' value = '"+Type+"'/>
</filter>;
formContext.getControl("project").addCustomFilter(ProjectFilter);

}

 

I get an error: "Web resource method does not exist: SetLookupField"

 

And I have no idea what I am doing wrong. If anyone can help me with this issue I would really appreciate it. 

 

Categories:
I have the same question (0)
  • Verified answer
    v-xida-msft Profile Picture
    on at

    Hi @Termigez1 ,

    Regarding the JavaScript code that you mentioned, I think there is something issue with it. For the 

    addCustomFilter()

    function, the first argument should be a string value. Please check the following article for more details:

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference/controls/addcustomfilter

     

    On your side, you should modify your formula as below:

    SetLookupField = function(executionContext) {
    formContext = executionContext.getFormContext();
    formContext.getControl("project").addPreSearch(FilterProject);
    }
    
    FilterProject = function() {
     var Country = formContext.getAttribute("country").getValue[0].id;
     var Type = formContext.getAttribute("type").getValue[0].id;
     var ProjectFilter = "<filter type = 'and'>
     <condition attribute = 'country' operator = 'eq' value = '"+Country+"'/>
     <condition attribute = 'type' operator = 'eq' value = '"+Type+"'/>
     </filter>"; // add a close " symbol here
     formContext.getControl("project").addCustomFilter(ProjectFilter);
    
    }

    The Fetch XML Query string value:

    "<filter type = 'and'>
    <condition attribute = 'country' operator = 'eq' value = '"+Country+"'/>
    <condition attribute = 'type' operator = 'eq' value = '"+Type+"'/>
    </filter>

    should be changed into following:

    "<filter type = 'and'>
    <condition attribute = 'country' operator = 'eq' value = '"+Country+"'/>
    <condition attribute = 'type' operator = 'eq' value = '"+Type+"'/>
    </filter>" // add a close " symbol here

     

    In addition, also please check if you have specified a SetLookupField method in your JavaScript code. Currently, the SetLookupField  method is not supported under the Client API of Model-Driven app. These available Client API supported in Model-Driven app as below:

    https://docs.microsoft.com/en-us/powerapps/developer/model-driven-apps/clientapi/reference

     

    Regards,

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 739 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 343 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard