
Announcements
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.
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:
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,