Custom Notifications aka Bell Notifications from the Apex code.
The Messaging.CustomNotification class allows you to create, configure, and send custom notifications using Apex code.
Here is a basic outline of how you can create and send a custom notification from an Apex trigger:
Create a Custom Notification Type: In the Setup, you should create a Custom Notification Type. This defines the type of notification you will be sending from Apex.
Use CustomNotification Class: Once you set up your Custom Notification Type, you can craft your Apex code to create the notification.
Here's an example code snippet:
// Get the Id for our custom notification type
CustomNotificationType notificationType = [SELECT Id, DeveloperName FROM CustomNotificationType WHERE DeveloperName='Lead_Due_Diligence_Completed'];
Messaging.CustomNotification notification = new Messaging.CustomNotification();
notification.setTitle('Your Notification Title');
notification.setBody('This is the detail of the notification');
notification.setNotificationTypeId(notificationType.Id);
notification.setTargetId('Record ID to which the notification refers, if applicable');
// To send the notification to a single user
notification.setRecipientIds(new List{'The Recipient User ID'});
// Send the notification try { notification.send(); }
catch { System.debug('Notification failed: ' + e.getMessage()); }
Please replace 'Your Notification Title,' and 'The Recipient User ID' with the actual values you intend to use.
Comments
Post a Comment