In this article, I demonstrate how easy it is to set the default value for a ComboBox control associated with a Dynamics 365 Lookup field in a Canvas PowerApp.
The demonstration application is a sample ‘Leave Requests’ Canvas PowerApp. The app uses the Common Data Service (CDS) connector to access a Leave Requests entity in Microsoft Dynamics 365.
The following image shows the app’s Edit Form open for adding a new Leave Request. There are no default values configured for any of the Lookup field controls on the form; i.e. Requester, Cover Person and Manager.
These ComboBox controls are bound to the corresponding Lookup fields from the Microsoft Dynamics 365 ‘Leave Requests’ entity.
The automatically generated formulas for the Default property of each of these Lookup field’s corresponding Data Cards are as follows:
For an existing Leave Request, “ThisItem.‘Requester’”, returns the currently selected Requester. However, for a new Leave Request, “ThisItem.‘Requester’” does not return any value. This is also the case for the ‘Cover Person’ and ‘Manager’.
The desired default value for Requester is the current User. Attempting to change the Default property formula, as shown in the following image, does not return any value, even though the User() function returns a record of information about the current User. This record only includes the User().Email, User().FullName and User().Image.
One approach is to retrieve the currently logged on User using the Choices function as shown in the following formula. Here, the formula uses the First, Filter and Choices functions to retrieve the first matching User, matched by Full Name and Primary Email. In this formula, “mag_RequesterId” is the physical name of the corresponding User Lookup field for the Requester.
An alternate approach is to add a CDS data source for Users to the app and then retrieve the currently logged on User from the Users data source, as shown in the following formula. Here the formula also matches by Status. The ‘Status (Users)’ option set enumeration is provided by the underlying CDS Users data source.
In this example, there is no desired default value for Cover Person. The User will select the Cover Person for themselves. Therefore, the formula for Cover Person can be retained as is.
The desired default value for Manager is the current User’s Manager.
Using this formula, similar to the first approach above, and with the addition of “.Manager”, does not return a value, even when the current User has a related Manager. Here, either “.mag_ManagerId” or “.mag_RequesterId” may be used in the Choices parameter.
However, this formula, similar to the alternate approach above, and with the addition of “.Manager”, does return the current User’s Manager when there is one.
As a result, when adding a new Leave Request, the default values for Requester, Cover Person and Manager are now set to the required default Lookup values.
Completing the other fields and clicking Accept, results in the successful creation of the new Leave Request.
In conclusion, as shown in this example, it is easy to set the default value for a Lookup field in a Canvas App. The approach shown here, however can be optimised to reduce the number of times the currently logged on User is retrieved from the underlying data source. In my next article I will show how this can be done.