Canvas Apps: What is a “scope” and, what does it have to do with Filter/Lookup functions?

By | March 31, 2020

In the previous post, I was looking at which attributes are added to the CDS query by the Filter/Lookup functions. Might not end up with a definitive answer there, but, to summarize, chances are you’ll get the attributes included IF those attributes are utilized in the other formulas.

Now, it turned out there is a caveat to the statement above.

Somehow, this is also tied to the record scope. In the example below, I have a label that’s displaying a text from the variable. That variable is set from within a button “OnSelect” in the gallery or from within another button “OnSelect” outside the gallery. Both “OnSelect”-s are using exactly the same formula:

Set(SelectedContact, LookUp(‘Contacts’, txtSearch.Text in ‘Full Name’ ));

and here is how label’s text is set:

“Selected Contact: ” & If(IsBlank(SelectedContact), “Blank”, SelectedContact.’Middle Name’)

As you can see below, the result of those clicks is quite different (just watch the label):scopes

Actually, when using that “in-gallery” button, apparently I’m not getting anything at all (not only the attribute, but even the record itself is not retrieved).

Why?

Again, I don’t have a complete explanation, but this is definitely related to the scopes. Because, you see, once the formula for that in-gallery button is updated to use disambiguation operator (“@”), I am starting to get the record (without the attribute yet).

Here is the updated formula:

Set(SelectedContact, LookUp([@’Contacts’], txtSearch.Text in ‘Full Name’ ));

Here is how it looks like now – notice that SelectedContact is not blank anymore, even though there is still no attribute value:

scopes1

Now, if I add that attribute to the formula (just about anywhere), I’ll finally start seeing it retrieved from the datasource. Here is another version of that formula (for the in-gallery button) – this may be the most important part of this post, btw:

image

And here is another recording:

scopes2

Finally, both buttons are working the same way.

So, what was the problem with the first version of my formula? Apparently, since I am using the gallery to display contacts, and since I am using a Lookup function to work with the same datasource, somehow those two are getting into a naming conflict. A disambiguation operator allows me to use global scope for the data source name:

image

https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/functions/operators#disambiguation-operator

However, even with that, and probably since it’s happening in a different scope, that in-gallery Lookup call does not know which attributes should be added to the request, and I have to mention all those attributes in the same scope.

And, of course, it’s just me reflecting on the ad-hoc workaround, so, even though it seems to make sense, I am not sure I’ve covered everything. What’s more important, I am still not sure of the exact “method” the framework is using to identify the attributes which will be added to the CDS query (but, at least, it seems I know, now, how to force it to add those attributes if some of them are missing).

 

 

One thought on “Canvas Apps: What is a “scope” and, what does it have to do with Filter/Lookup functions?

  1. Arvind

    Thanks a ton for this explanation – didn’t find anywhere in the Microsoft docs in my modest search. They should mention it somewhere.

    Reply

Leave a Reply to Arvind Cancel reply

Your email address will not be published. Required fields are marked *