cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Termigez1
Helper I
Helper I

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.

Helpful resources

Announcements
PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Top Solution Authors
Top Kudoed Authors
Users online (2,305)