Background
For the past month or so, I have been working on an app that shows an org structure, among other things. At a very high level, you select a user (from a gallery) to then view their manager and direct reports. The app uses one of my favorite connectors - Office365Users. While it provides a lot of information about users, it does have its own weird behavior and errors.
Issue - runtime errors
Once a user is selected, the following steps happen:
- The manager of the selected user is fetched and populated into a collection
- The selected user is added to the same collection, and finally,
- The direct reports of the selected user are added to the same collection
The problem is Office365Users connector throws an error when you use the GetManagerV2 function and the user that is passed on as a parameter to the function does not have a manager. I created a simplified version of the app to illustrate this issue.
Here is how the app behaves when there is a manager for the selected user:
Here is how the app behaves when there is no manager for the selected user:
Here is a screenshot of the error message that the end user sees:
The app maker also sees this within the studio:
And the app checker does its part too:
My initial thought was to simply check if the function returned a manager or not so I tried the following expressions but none of them worked:
- IsError:
IsError(Office365Users.ManagerV2(ThisItem.mail))
- IsBlank:
IsBlank(Office365Users.ManagerV2(ThisItem.mail))
- IsEmpty:
IsEmpty(Office365Users.ManagerV2(ThisItem.mail))
Resolution - error management
To have the ability to "catch" this error (and other similar ones), turn on the following experimental feature:
- Formula-level error management: Create custom errors to improve app usability and gather valuable error information
With this feature turned on, I can use something like this in the OnSelect of the users gallery:
If(
IsError(Office365Users.ManagerV2(ThisItem.Id)),
Notify(
ThisItem.DisplayName & " has no manager defined in Office365.",
NotificationType.Warning
)
)
Here's how the app behaves with the experimental feature turned on and the addition of the above code:
Summary
To summarize, if you want to have more control over errors in your apps to provide better user experience, turn on the Formula-level error management experimental feature. Also note that to write blank values to a database, you need this experimental feature. This is referenced in one of my previous blog posts here and in Microsoft Docs here.
6 thoughts on “How to create custom errors in Power Apps!”