Send Alerts to Salesforce Users Through Custom Bell Notifications
You can create alerts or notifications for desktop and mobile users in Salesforce using the custom notification feature.
Join the DZone community and get the full member experience.
Join For FreeYour sales agents or service reps are busy managing their workflows in Salesforce. Timely attention to key events is necessary for these users to be effective in their roles. Alerts or notifications play a crucial role in providing real time information for Salesforce users and their managers on key changes that need immediate attention. For example, you can alert sales agents regarding key changes in their opportunities or service agents on cases that need immediate attention. This article talks about different ways you can set up notifications in Salesforce.
Salesforce's bell notifications for users are available for both desktop and mobile apps. You can customize the title and body for these notifications so that it suits your business use case. You can also define a target navigation page so that when a notification is clicked, the user lands on that page. You have two options for navigation target — to a record page or a page reference in Salesforce. You must specify one target for your notification.
You can create notifications in three different ways.
- REST API
- Apex
- Flow
The first step for bell notification is to set up a 'custom notification' metadata.
Required Attributes for Custom Notification
- Title: Title of the notification.
- Body: Message body of the notification.
- Notification Type ID: Required. ID of the notification type we created above. You can get it by following SOQL.
Select id,CustomNotifTypeName, DeveloperName from CustomNotificationType where DeveloperName = 'Email_Alert'
4. Recipient ID: ID of the recipient (user) or recipient type (account ID, opportunity ID, group ID, queue ID).
5. Target: This could be a record ID or a page reference. Either one should be specified.
Create Notifications via REST API
You can create notifications using REST API. This is useful when you need to alert your sales agents regarding any events that are external to Salesforce. An external system can ping Salesforce using this REST API to alert agents. Following are the API details.
- HTTP Method:
POST
- URI:
/services/data/vXX.X/actions/standard/customNotificationAction
- Payload: If the target is recorded Id.
{
"inputs":[
{
"customNotifTypeId":"0MLR0000000008eOAA",
"recipientIds":[
"005R0000000LSqtIAG"
],
"title":"Your opportunity is changed",
"body":"Please jump to your pipeline management",
"targetId":"001R0000003fSUDIA2"
}
]
}
If the target is page reference:
{
"inputs":[
{
"customNotifTypeId":"0MLR0000000008eOAA",
"recipientIds":[
"005R0000000LSqtIAG"
],
"title":"Your opportunity is changed",
"body":"Please jump to your pipeline management",
"targetPageRef": {
type: 'standard__navItemPage',
attributes: { apiName: 'MyCustomTabName'}
}
}
]
}
Create Notification via Apex
You can create notifications using Apex. This is useful when your events are within Salesforce and if you want to trigger creating notifications using an apex trigger or batch jobs. Following are the details.
- Create an instance of
Messaging.CustomNotification
using the default constructor. - Use different setter methods to set the required attributes for the custom notification.
Messaging.CustomNotification customNotificationObj = new Messaging.CustomNotification();
Id userId = Userinfo.getUserId();
customNotificationObj.setBody('Please jump to your pipeline management');
customNotificationObj.setTitle('Your opportunity is changed!!');
CustomNotificationType type = [select id,CustomNotifTypeName, DeveloperName, Description from CustomNotificationType where DeveloperName = 'Email_Alert'];
customNotificationObj.setNotificationTypeId(type.id);
customNotificationObj.setSenderId(userId);
String addressTest =
'' +
' {' +
' type: \'standard__navItemPage\', ' +
' attributes: {' +
' apiName: \'Pipeline_Management\'' +
' }'+
' }'+
'';
customNotificationObj.setTargetPageRef(addressTest);
customNotificationObj.send(new Set<String> {userId});
The important thing to note here is that the send method can be used only for one notification at a time. It cannot be used to send multiple notifications at a time.
Create Notifications via Flow
You can also create notifications without a single line of code using flows.
Conclusion
Salesforce bell notifications can be used to alert your users on key events that need timely attention. There are three different ways you can create notifications in Salesforce — API, Apex, or Flow. You can customize these notifications with a title and body that suits your business use case. You should also define target navigation for these notifications so that users land on an appropriate page for further actions.
Opinions expressed by DZone contributors are their own.
Comments