Monday, May 10, 2021

Querying for Custom Notifications in Salesforce

 

a screenshot of Salesforce notification area with one notification

A colleague was trying to retrieve a list of custom notifications that was sent to them during the day.  They were running a set of Apex jobs that throw custom notifications via Apex when there's an issue.  As things piled up, it's probably nice to export a list of notifications for record keeping!  It turns out, not so easy.  Custom Notification seems to be designed to be transient and not too much support for easy backend access.  In the end, we settled on using the Connect REST API.  Workbench was the first tool tried, but it just returned an empty Notifications list.  I suspected that it's result of the nature of notifications, combined with the fact that Workbench is connecting as a Connected App, which isn't subscribing to any notification.   Since Workbench's app is managed, it can't be updated with subscriptions.  Time for some workaround. 

I decided to hit the API directly using a browser session ID, so the API will hopefully treat it pretty much like a browser session.  I was on a plain Windows laptop so Invoke-RestMethod cmdlet is the most convenient (I'd imagine curl works just fine on OSX).  The command is simple for this one.

Invoke-RestMethod https://instanceURL/services/data/v51.0/connect/notifications -Method GET -Headers @{"Authorization" = "Bearer sessionId")

After firing a few test notifications by Apex, I ran the command and got the notification records as expected!   The only additional step I had to take to do is to pipe the output to Format-Table -width 300 - otherwise PowerShell just cut off output after the first line.  There's probably better way to collect the output but I didn't try further as it's just a POC for now.


No comments:

Post a Comment