custom notifications

Custom Notification Using Apex | Salesforce Developer Guide

Custom Notification 

Custom notification may be a made-to-order notification within Salesforce org. We are able to use this feature to send notifications once any vital event happens. for instance, the record owner is notified once the record is approved by his manager. Or another example is just like the user  is notified once a replacement lead is generated. 

Where Do We Find Custom Notifications? 

Custom notification can be found on the bell icon inside the Salesforce user interface. 

How to send custom notifications using apex?  

  • Type 'Notifications' in the quick find box in setup. 
  • Click on  'Custom Notifications'. 
  • Click on the 'New' button on the right  side. 
  • Enter your custom notification name and API name. 
  • Select the channels (Mobile and/or Desktop). 

dont miss out iconDon't forget to check out: Deleting Apex Classes / Apex Triggers From Production Using Workbench | Salesforce Tutorial

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 an instance code, which we use to create and send the custom notifications. This class contains the methods to set the different parameters of the custom notification. 
  • The setBody, setTitle methods are used to set the title of the notification. These are mandatory. 
  • setSenderId is the Id of a user who is sending the custom notification, this is not mandatory. 
  • setNotificationTypeId sets the CustomNotificationType object. This is mandatory. 
  • setTargetId the method sets the target id of the notification. Upon notification click, the user is redirected to the record page of the record id set here. This is mandatory. 
  • send the method is used to send the custom notification from the code. 

Example Code to Send Custom Notification  

Class :- 

/*---------------------------------------------------------------------------------------------------------------------- 
This class shows how to create custom notification using apex 
Functionality : This class contains handler method for a trigger to send custom notification whenever a 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}); 
            } 
        } 
    } 
}

dont miss out iconCheck out another amazing blog by Romil here: Learn All About Workflow Rules in Salesforce

Trigger :-  

trigger OpportunityNotificationTrigger on Opportunity (after update) { 
    if(Trigger.isAfter && Trigger.isUpdate){ 
        OpportunityNotificationHandler.SendNotification(trigger.new); 
    } 
}

 

Responses

Popular Salesforce Blogs