How to send notifications to bell icon in Salesforce

Salesforce allows us to send custom notifications to bell icon in lightning experience using "custom notification" feature. These custom notifications can be sent using process builders or flows or REST API.

Unfortunately there is no native method in apex that you can just call to send notification yet. One work around is to make a a callout to Salesforce rest endpoint from Apex. In this blog we will go through examples of sending custom notifications through different channels.

Use cases

  • Can be used to send notifications to desktop/lightning experience bell icon or to Salesforce mobile app as push notification
  • Any scenario of when you need to get user's attention in Salesforce. For example, notify sales agent about an activity on an opportunity they are working on, notify administrator about failures in a critical API call etc

Using apex to send notification


public class NotificationManager {
public static Id notificationId = [Select Id, CustomNotifTypeName from CustomNotificationType WHERE CustomNotifTypeName = 'Error Notification' LIMIT 1].Id;
//Method to send notifications
public static void sendNotification(List<Id> receipients, String title, String body, Id targetId) {
Http http = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint(Url.getOrgDomainUrl().toExternalForm() + '/services/data/v48.0/actions/standard/customNotificationAction');
req.setMethod('POST');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');
//Constructing payload
Map<String, Object> notificationObjMap = new Map<String, Object>();
notificationObjMap.put('title', title);
notificationObjMap.put('body', body);
notificationObjMap.put('customNotifTypeId', notificationId);
notificationObjMap.put('targetId', targetId);
notificationObjMap.put('recipientIds', receipients);
Map<String, Object> payload = new Map<String, Object>{'inputs' => new List<Object>{notificationObjMap}};
req.setBody(JSON.serialize(payload));
HttpResponse res = http.send(req);
System.debug(res.getBody());
}
}

Explanation

Salesforce still hasn't made the ability to send custom notifications from apex available in apex. So we need to call Salesforce REST API (Custom Notification Actions) from apex to to trigger the notifications.

In this example we are expecting a custom notification to be created from setup with name Error Notification. Once you have a the custom notification setup, you can just call the method using below syntax.


List<Id> recipientList = new List<Id>{Userinfo.getUserId()}
String title = 'Please check the opportunity';
String body = 'Important change in opportuntiy';
Opporutnity opp = [SELECT Id FROM Opportunity LIMIT 1];
NotificationManager.sendNotification(recipientList, title, body, opp.Id);

Steps to send notification through configuration

  1. Create a custom notification from setup


  2. Create a process builder entry to fire on updates of opportunity records


  3. Example. Update any opportunity and see result in bell icon


No comments:

Post a Comment