How to enable Custom Notification using Apex?

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? 

  1. Type "notifications" in the setup quick search box.
  2. Click Custom Notifications.
  3. Click the New button on the right.
  4. Enter a custom notification name and API name.
  5. Select a channel (mobile and/or desktop). 
  6. Use below code to send Custom Notification 
Messaging.CustomNotification notification = new Messaging.CustomNotification(); 
notification.setBody('Sample Body'); 
notification.setTitle('Sample title'); 
CustomNotificationType type = [SELECT Id FROM CustomNotificationType WHERE DeveloperName = 'MyCustomNotificationName']; 
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. 

dont miss out iconDon'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, 
                                                        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.setTitle('Opportunity Closed Won'); 
                Notification.send(new Set<String> {opp.OwnerId}); 

dont miss out iconCheck out another amazing blog by Navdita here: How to Update the Values of a Custom Picklist Field using Metadata API?


trigger OpportunityNotificationTrigger on Opportunity (after update) { 
    if(Trigger.isAfter && Trigger.isUpdate){ 


Popular Salesforce Blogs