Today we’re going to look at how you can set a default selection using Dataverse Lookup fields in Canvas Power Apps. Why would you want a default field selected? Well, if you’re using a lookup field to a User() entity, you may want to auto-select the logged in user rather than forcing the User to search for their own name, this is also the example I’ll be using today.
App.OnStart
We need to firstly assign the user profile to a variable. Why would we do this? This is to mitigate delegation issues, if you want to read more about what ‘Delegation’ means then you can check it out on the Microsoft Website here.
To do this we use the Set function to assign the Office365Users.MyProfile() to a variable named office365UserReference. See the code below:
Set(office365UserReference, Office365Users.MyProfile())
DataCardValue
Now we have the a variable with the user profile we can begin the default selection process. Firstly, you want to select the DataCardValue item within the DataCard and Form that you are using. In this example, I would have an employee entry form, a User data card and inside this DataCard I have a DataCardValue (The drop-down selection/combo box).
Now you want to select ‘Item’ on the fields list and copy the Choices[@Example].User (what ever yours is). Once copied select ‘DefaultSelectedItems’.
Now we’re on the DefaultSelectedItems, we need to add some logic to grab the first result that contains my profile and select it. To do this we will use the First() function to select the first returned item, Filter() to allow us to filter our profile from the list of profiles by finding our display name (from the variable profile we set) in the User() Full Name field. See the code below:
First(Filter(Choices([@Example].User), office365UserReference.DisplayName in 'Full Name'))
Finished!
That’s it! To actually see this live in your application do not forget to run ‘Run OnStart’. We can now default select an item (in this example the logged in user) from a Dataverse Lookup field. You can of course customize this to work with any lookup, just remember to grab the choices code from the ‘Items’ and apply the relevant filter in the DefaultSelectedItems field.