How To Trigger Custom Notification Using Apex In Salesforce?

How To Trigger Custom Notification Using Apex In Salesforce?
In this blog, you will learn how you can trigger custom Lightning notifications using apex in salesforce.

Follow below simple steps

  • Go to Setup
  • Search for Custom Notification
  • Create a New Custom Notification and Save

  • Go to Setup
  • Open Developer Console
  • Go To Debug menu
  • Select Open Execute Anonymous Window from dropdown

Account accObj = new Account();
accObj.Name ='SFDC SAGA';
Insert accObj;

CustomNotificationType notificationType = [SELECT Id FROM CustomNotificationType WHERE DeveloperName = 'Notification'];
Messaging.CustomNotification notification = new Messaging.CustomNotification();
notification.setTitle('New Account Created - ' + accObj.Name );
notification.setNotificationTypeId( notificationType.Id );
notification.setBody('click here to navigate to the account record.');
notification.setTargetId(accObj.Id);
try{
    notification.send(new Set<String> { accObj.OwnerId });
}
catch (Exception e) {
System.debug('Problem sending notification: ' + e.getMessage());
}

  • Click on Execute

Demo


Follow Us

Posted By : Sudeer Kamat Date :

view_module Related

label Labels

Comments 0