Send Custom Notification using Apex in Salesforce

In this post, we will implement the functionality to Send Custom Notification using Apex in Salesforce. This feature is introduced recently in the Winter ’21 release. We can also send the Custom Notification using Process Builder in Salesforce. You can check the complete implementation for that here.

Implementation

In this implementation, we will Send Custom Notification using Apex in Salesforce once the Opportunity Stage is changed to Closed Won using Apex Trigger. The notification will be sent to the Owner of the Opportunity.

Send Custom Notification using Apex in Salesforce
Send Custom Notification using Apex in Salesforce

Create Custom Notification Type

First, we need to create a Custom Notification Type in Salesforce.

  • Type Custom Notification in the Quick Find box and click on Custom Notifications under Notification Builder.
  • Provide Custom Notification Name, API Name, and Supported Channels and click Save.

It should look something like below:

Custom Notification Type in Salesforce
Custom Notification Type in Salesforce

Send Custom Notification using Apex

To send a Custom Notification using Apex, we need to use the send() of CustomNotification class. We can create a Custom Notification either using a Parameterized Constructor of Messaging.CustomNotification and passing the required parameters or by using Default Constructor and use the methods of Messaging.CustomNotification Class to set the required parameters. In this implementation, we will use a Parameterized Constructor.

Parameterized Constructor accepts below parameters:

  • typeId: Id of the Custom Notification Type that we created earlier. [Required]
  • sender: User Id of the sender of Notification. [Optional]
  • title: Title of the Notification. [Required]
  • body: Body of the Notification. [Required]
  • targetId: the Record Id for the target record of the notification.
  • targetPageRef: PageReference for the target record. [Either targetId of targetPageRef is required so that when the user clicks on the Notification, the user can be redirected to the target record].

Once the CustomNotification record is created, we can use the send(recipientIds) method to send the Notification where recipientIds is the Set of Strings which contains the Ids of Recipients.

OpportunityTrigger

We are using an Apex Trigger to trigger the notification. Please note that the below Apex Trigger does not follow Best Apex Trigger Practices as it is not the focus of this implementation. If you want to know about Best Apex Trigger Practices, you can check it here.

trigger OpportunityTrigger on Opportunity (after update) {
    CustomNotificationType closedWonNotification = [SELECT Id, DeveloperName 
             								FROM CustomNotificationType 
             								WHERE DeveloperName='Opportunity_Closed_Won' LIMIT 1];
    
    if(Trigger.isAfter && Trigger.isUpdate){
        for(Opportunity op : Trigger.New){
            if(op.StageName == 'Closed Won'){
                String body = 'Opportunity ' +op.Name +' is Closed Won.';
                Messaging.CustomNotification newNoti = new Messaging.CustomNotification(
                    closedWonNotification.Id, null, 'Opportunity Closed Won', body, op.Id, null
                );
                try {
                    newNoti.send(new Set<String> {op.OwnerId});
                }
                catch (Exception e) {
                    System.debug('Problem sending notification: ' + e.getMessage());
                }
            }
        }
    }
}

That is all from this implementation. This is how we can Send Custom Notification using Apex in Salesforce.

If you don’t want to miss new implementations, you can Subscribe here.

If you want to know more about Custom Notification using Apex, you can check official Salesforce documentation here.

Thank you for reading. See you in the next implementation.