cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Termigez1
Frequent Visitor

Filter lookup field using addCustomFilter control with multiple conditions

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. 

 

1 ACCEPTED SOLUTION

Accepted Solutions
v-xida-msft
Community Support
Community Support

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

 

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,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

1 REPLY 1
v-xida-msft
Community Support
Community Support

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

 

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,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (1,737)