
Bill Cat pondering how to get the logged in user in a Canvas app.
I learned something new today thanks to the Internet (shocking, right?)
Yesterday one of our team members was showing me an error she was having in our Power Apps model-driven app that’s our project tracking system. We have a comment feature that uses the Pages feature of Canvas apps. She couldn’t leave a comment on a task AND she’d receive an error when viewing past comments.
The not being able to leave a comment was my fault (I changed security permissions that caused this). Whoops!
The error when trying to view an existing comment was something else entirely: a bug (of my creation)! I’ll explain further.
When a new comment is left for someone, they receive an email of the comment with a link to the task. In the task, there is a pop-up screen of all the comments. Anything unread is in bold.

Behind the scenes, the new comment is a record in a table called “Comments” and has a custom status of “Unread (or active).” Clicking the view button will show the text of the comment and also run a Patch() statement to change the status of the comment to “Read” (or inactive). Essentially I am replicating how any email client works with read/unread messages.
WELLLLLLLLLLLLLLLL…
Anyone clicking on the comment would be marking it as “Read”, so the person the comment was left for would not see it as unread! Honestly, not a huge deal, but a user experience issue nonetheless. So, how to let people read comments, but not mark it as read if they are not the recipient of the comment?
The AsType() and User() functions to the rescue!
On the OnSelect of the View button, this statement handles it all — check if the Owner of the comment is the same as the logged in user, if so, then run a Patch() statement to set the status of the comment as read, otherwise, just display it:
If(AsType(galComments.Selected.Owner, Users).'Primary Email' = User().Email,
Patch(Comments, galComments.Selected, {Status: 'Status (Comments)'.Inactive, 'Status Reason': 'Status Reason (Comments)'.Read}));
Set(varCommentText, ThisItem.Text)
Pretty neat!
Since I was on a roll with this, we decided to tackle adding a delete button to a comment.

Now you see it…
Once again, the User() function saves the day!
The little trash can icon is conditionally visible by the creator of the comment. If you’re the creator, you can delete it, otherwise you can’t. Add this to the Visible property of the icon to check the creator of the item:
If(ThisItem.'Created By'.'Primary Email' = User().Email,true,false)

Now you don’t…
That’s it.
I love simple solutions. Thank you Internet for teaching me something new!
