How to enable Custom Notification using Apex?
What is a Custom Notification?
A custom notification can be a custom notification within your Salesforce org. You can use this feature to send notifications as soon as important events occur. For example, when a record is approved by a manager, the record owner is notified. Or another example would be to notify the user as soon as a replacement lead is generated.
Where do we find Custom Notifications?
Custom notifications can be found on the bell icon inside the Salesforce user interface.
How to Send Custom Notifications using Apex?
- Type "notifications" in the setup quick search box.
- Click Custom Notifications.
- Click the New button on the right.
- Enter a custom notification name and API name.
- Select a channel (mobile and/or desktop).
- Use below code to send Custom Notification
Messaging.CustomNotification notification = new Messaging.CustomNotification(); notification.setBody('Sample Body'); notification.setTitle('Sample title'); notification.setSenderId(Userinfo.getUserId()); CustomNotificationType type = [SELECT Id FROM CustomNotificationType WHERE DeveloperName = 'MyCustomNotificationName']; notification.setNotificationTypeId(type.id); notification.setTargetId('006B000000005hCxzIAE'); // target object id notification.send(new Set<String> { Userinfo.getUserId() });
Messaging.CustomNotification is the instance code used to create and send custom notifications. This class contains methods for setting various parameters for custom notifications.
- The setBody, and setTitle methods are used to set the title of the notification. These are required.
- setSenderId is the ID of the user sending the custom notification. This is not required.
- setNotificationTypeId sets the CustomNotificationType object. This is required.
- The setTargetId method sets the notification's target ID. Clicking the notification will take the user to the record page with the record ID specified here. This is required.
- The send method is used to send custom notifications from your code.
Don't forget to check out: Deleting Apex Classes / Apex Triggers From Production Using Workbench | Salesforce Tutorial
Example code to send Custom Notification
This class shows how to create custom notifications using Apex
Functionality: This class contains a handler method for a trigger to send a custom notification whenever an opportunity's stage name is changed to Closed Won
public class OpportunityNotificationHandler { public static void SendNotification(List<opportunity> OppList){ CustomNotificationType closedWonNotification = [SELECT Id, DeveloperName FROM CustomNotificationType WHERE DeveloperName='Opportunity_Custom_Notification' LIMIT 1]; for(opportunity opp : OppList){ if(opp.StageName == 'Closed Won'){ String body = 'Opportunity ' +opp.Name +' is Closed Won.'; Messaging.CustomNotification Notification = new Messaging.CustomNotification(); Notification.setNotificationTypeId(closedWonNotification.id); Notification.setSenderId(Userinfo.getUserId()); Notification.setBody(body); Notification.setTitle('Opportunity Closed Won'); Notification.setTargetId(opp.Id); Notification.send(new Set<String> {opp.OwnerId}); } } } }
Check out another amazing blog by Navdita here: How to Update the Values of a Custom Picklist Field using Metadata API?
Trigger
trigger OpportunityNotificationTrigger on Opportunity (after update) { if(Trigger.isAfter && Trigger.isUpdate){ OpportunityNotificationHandler.SendNotification(trigger.new); } }
Responses