Retrieving and Updating Custom Notifications using Connect Rest API

Notifications any user receives that appear under the Notifications Bell Icon, unlike any other records in Salesforce, cannot be queried using SOQL or updated using DML statements.  

When business requires us to show the notifications in a customized format in a custom component, it will be a challenge. These custom notifications can only be retrieved using REST API calls and changed into the required format to be shown in custom components. You can find the details of methods and parameters for this API here.  

Retrieving Notifications 

The ‘connect/notifications’ can be used to retrieve the Custom Notifications received by a user. The notifications of the user who makes the API call alone will be returned, not everyone’s.  

On hitting the above endpoint with the HTTPRequest Get method, you can find the notifications with all its parameters present. This API call retrieves only the latest twenty notifications by default. A size attribute can also be added with a value between 1 and 20. To retrieve earlier notifications the before attribute can be specified with a datetime String value. 

Below is an example of a wrapper class and how the notifications can be passed on to LWC. 

public static List<Object> fetchNotifications(){ 

        String sfdcURL = URL.getOrgDomainUrl().toExternalForm(); 

        String restAPIURL = sfdcURL + ‘/services/data/v51.0/connect/notifications?size=20’; 

        HttpRequest httpRequest = new HttpRequest();   

        httpRequest.setMethod(‘GET’); 

        httpRequest.setHeader(‘Authorization’, ‘Bearer ‘ + UserInfo.getSessionID());  

        httpRequest.setEndpoint(restAPIURL);    

        String response = ”;  

try {  

Http http = new Http();  

HttpResponse httpResponse = http.send(httpRequest);  

if (httpResponse.getStatusCode() == 200 ) { 

 response = JSON.serializePretty( JSON.deserializeUntyped(httpResponse.getBody()) );  

system.debug(‘ Connect Notifications—> ‘ + response);  

}  

 catch(System.Exception e) { 

  System.debug(‘ERROR: ‘+ e);  

The variable ‘response’ holds the notifications retrieved. The notifications thus retrieved can be shown in a custom LWC too. For that, we can modify the retrieved notifications into a list of wrapper class and pass it to LWC. 

Updating Notifications 

Using Patch() we can update the notifications. The read and seen fields alone can be updated.  

The same endpoint ‘/connect/notifications’ is used for multiple notifications and ‘/connect/notifications/notificationId’ can be used for updating a single notification using Patch().  

try{ 

          String  notiId; //ID of the notification 

           String sfdcURL = URL.getOrgDomainUrl().toExternalForm(); 

            String restAPIURL = sfdcURL + ‘/services/data/v51.0/connect/notifications/’+notiId+’?read=true’; 

            HttpRequest httpRequest = new HttpRequest();   

            httpRequest.setMethod(‘PATCH’); 

            httpRequest.setHeader(‘Authorization’, ‘Bearer ‘ + UserInfo.getSessionID());  

            httpRequest.setEndpoint(restAPIURL);   

            Http http = new Http();    

            HttpResponse httpResponse = http.send(httpRequest);   

        } 

        catch(Exception ex){ 

            System.debug(‘ERROR:’+ex); 

        } 

The above code snippet can be used to update Read as true in the notification. 

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.