Before you start reading, here is a disclaimer right away: this post is not an extension to the power apps documentation. It is just my attempt to figure out how Lookup and Filter functions work in Canvas Apps when those functions are applied to the CDS data source. I am hoping the information below is accurate, but use it at your own risk.
Was it scary enough?
Let’s continue to the fun part, then.
Here is a question: when you have a Filter function in your canvas apps against “Contacts” entity, which attributes will each of the returned records have?
Luckily, Canvas Apps are still web apps, so I can fire up chrome dev tools and do some network tracing.
But, first, let’s talk about the setup.
I have a simple Canvas App that’s using a Gallery control to display contact names and email addresses. There is a text box where I can enter a keyword, and that keyword will be used to filter records in the Gallery:
Now, I’m using “Select” button to set “SelectedContact” variable:
In turn, I’m using that variable to display selected contact phone number:
Based on the setup above, I have “Contacts” datasource in my app:
However, if you look at the above screenshots, I am not specifying the query attributes anywhere. There is no FetchXml, there is no Column Set (for those of you familiar with the SDK), there is no “select” parameter (Web API?), etc. So how does the framework know that it needs to query Phone Number attribute, for example?
Quite frankly, I don’t know But I can guess.
When looking at the query that the app sends to the Web API endpoint once the search keyword is updated, I see this:
Apparently, this includes all the fields I’m using on that screen. Let’s try something , though. Let’s replace ‘Home Phone’ with ‘Middle Name’ in the label’s text formula:
If I just “play” the app right away, there will be no text displayed in the label:
However, if I change the search text so a new request is sent to the server, and, then, click “select” button for exactly the same contact, here is what I’ll get:
What happened? From the dev tools trace, it’s clear that ‘Middle Name’ attribute was added to the query:
Although, ‘Home Phone’ (telephone2) is still there as well.
Hm… Let’s close the browser, completely. Then let’s open it again and re-run the app. Here is how the trace looks like this time:
There is middlename, but there is no telephone2.
I think at this point it’s clear what the framework is doing. It’s looking at all the attributes you might be using in different places, and it’s adding them to the query. Those attributes stick to the session, so, if you stop using some of them, they will still be added to the query until there is another session, at which time the framework will update the list of attributes.
However, there seem to be some optimization there, and it might be unexpected. What if I used Filter in a different place to populate a different collection?
Since my app is not using other attributes through that collection, only contactid and ‘Full Name’ attributes get added to the query:
Now what if I take it one step further?
Technically, I seem to be doing the same what I was doing before. I am setting “SelectedContact” from the collection, so I would expect ‘Middle Name’ to show up in the label text. Instead, that label does not show anything now:
How about I collapse that formula into a single line?
Now I’m getting somewhere, it seems:
Except that… My label is absolutely empty now:
I need to go there, update the “Text” property of that label, and, then, the text shows up:
Fortunately, this only happens once when following the steps above.
Still, the fact that I have to collapse my formula into a one liner means that the framework has limited ability to infer the attributes it needs to add to the query.
It seems that, as soon as the attribute is utilized “directly”, everything works. But, once there is an extra variable between the query and the place where that attribute is utilized, this mechanism stops working. For example, it stops working once “OnSelect” formula is modified this way:
Which is obvious from the dev tools – there is no “middle name” attribute there:
Does this provide a definitive answer? Not really, but, quite frankly, I’ve been struggling with this behavior in my application recently, and, so I figured it would make sense to dig into it and see what’s happening. Not sure if it helps at the moment, but… knowledge is power, too
PS. There is a continuation to this post here: https://www.itaintboring.com/powerapps/canvas-apps-what-is-a-scope-and-what-does-it-have-to-do-with-filterlookup-functions/