Handling Blank Managers in AAD with the Office365Users Power Apps Connector

A project I worked on recently required me to be able to auto populate a form field / combobox with another persons line manager based on either the current user logged in, or a selected user in another combobox. This further option of the… READ MORE [https://lewisdoes.dev/blog/handling-blank-ma
Handling Blank Managers in AAD with the Office365Users Power Apps Connector
In: Low Code Lewis Content 🚀

A project I worked on recently required me to be able to auto populate a form field / combobox with another persons line manager based on either the current user logged in, or a selected user in another combobox. This further option of the current user or selected user was dependent on a toggle being switched which asked users whether the form being filled out was about themselves.

One of the issues I was running into was where users didn’t have a manager populated in AAD. This caused the Office365Users connector to return an error from Azure. So, I needed a way to handle this error and make Power Apps return a blank instead of an error which it didn’t understand. I reached out to a colleague and the community on this as I wasn’t sure what to do to start with. Credit to Pieter Veenstra, BCBruizer in Power Apps forums and Kristine Kolodziejski for various parts of this solution which jogged my thoughts to coming up with the fix that worked!

It is important to note that to handle blank line managers in AAD when using the Office365Users Power Apps connector, I am using an experimental feature here that you probably don’t want to use in Production.

What are we using here?

So, the error that Azure returns when we try to lookup someone’s manager but they have a blank value is as follows:

To handle this error, we’re going to use an experimental feature. I’ll say again though… be careful with using these kind of features in Production! But if and when they go to GA… go nuts!

In Power Apps, within the studio, we’re going to want to go to ‘settings’ -> Upcoming features -> Experimental -> and then turn on ‘Formula-level error management’

Error, IfError, IsError, IsBlankOrError functions in Power Apps

For more information on the functions we are able to get access to by turning on this experimental feature, check out the following documentation on Error, IfError, IsError and IsBlankOrError functions in Power Apps.

Error, IfError, IsError, and IsBlankOrError functions in Power Apps – Power Platform | Microsoft Docs

For this error handling example, we’re going to use the IfError() function.

Using IfError() to handle missing managers in AAD

So, moving on to how we use IfError() to handle missing managers in AAD. Again, credit goes to others listed at the start of this post who suggested using this function to handle the errors here. I still wasn’t quite managing to handle the errors for a short while though. I tried a couple of different things.

In my case, I was using the DefaultSelectedItems property of my combobox control to effectively auto fill or lookup my manager based on another value. To do this we need to provide the property with the record of our manager. We do this in the following format:

{ '@odata.type': "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser", Department: Office365Users.ManagerV2(DataCardValue3.Selected.Email).department, Claims: "i:0#.f|membership|" & Office365Users.ManagerV2(DataCardValue3.Selected.Email).mail, DisplayName: Office365Users.ManagerV2(DataCardValue3.Selected.Email).displayName, Email: Office365Users.ManagerV2(DataCardValue3.Selected.Email).mail, JobTitle:Office365Users.ManagerV2(DataCardValue3.Selected.Email).jobTitle, Picture: "" }

In my case above, in this part of my code I am retrieving the user I want to find the manager of using the selected item in another combobox. You could also use the function User() for example.

As I say, here I tried a couple of different things to handle the returned error from Azure. I tried putting my IfError() function around the entire syntax in my DefaultSelectedItems property, and I tried putting it around my record that you see above.

Neither of these methods worked, and then I noticed something. This really isn’t the right syntax to use! Remember in this kind of record syntax we’re declaring columns and their values, so the error will only be on the returned value, it won’t have come from the wider record/syntax. You can further confirm this by selecting the code in the Power Fx editor in Power Apps where you will see the record appear. You’ll notice it doesn’t just return one error, but the error is returned for each of the columns.

The correct syntax

So, what is the correct way to handle the errors? Bearing in mind they’re being returned from the value retrieved on each individual column, not the wider record, or wider syntax, we need to actually put the function around the values for each column of the record. Therefore the correct syntax is as below:

{ '@odata.type': "#Microsoft.Azure.Connectors.SharePoint.SPListExpandedUser", Department:IfError( Office365Users.ManagerV2(DataCardValue3.Selected.Email).department,Blank()), Claims: IfError("i:0#.f|membership|" & Office365Users.ManagerV2(DataCardValue3.Selected.Email).mail,Blank()), DisplayName: IfError(Office365Users.ManagerV2(DataCardValue3.Selected.Email).displayName,Blank()), Email: IfError(Office365Users.ManagerV2(DataCardValue3.Selected.Email).mail,Blank()), JobTitle: IfError(Office365Users.ManagerV2(DataCardValue3.Selected.Email).jobTitle,Blank()), Picture: "" }

We are now handling the error which each appropriate column is returning. Note we don’t need to do this for the ‘@odata.type’ column and ‘Picture’ column as these are hard coded and aren’t using Office365Users.ManagerV2.

So using the following syntax: IfError(Value,Blank())

We’re now saying If there isn’t an error with the ‘Value’, return ‘Value, and if there is return ‘Blank()’. You can change Blank() to something alternative if it suits you! But in my case if the user I’m trying to find a manager for, has a blank manager, I want Power Apps to show the manager as blank, which is what we have now achieved here!

I hope this post helped, if you have any comments or questions, pop them down below!

Written by
Lewis Baybutt
Microsoft Business Applications MVP • Power Platform Consultant • Blogger • Community Contributor • #CommunityRocks • #SharingIsCaring
Great! You’ve successfully signed up.
Welcome back! You've successfully signed in.
You've successfully subscribed to LewisDoesDev.
Your link has expired.
Success! Check your email for magic link to sign-in.
Success! Your billing info has been updated.
Your billing was not updated.