Code Way You to start with creating a Salesforce custom notification, enable Lightning Experience & Mobile App notifications!!
Custom Notification is very important to send alerts on desktop and/or mobile & keep your users in the know when important. Sometimes it is necessary to use custom Apex code instead using Process Builder or Flow.
Here I tried to write code with schedular to run autonomously as cron job by Apex Schedular Class.
This Example will show how to trigger Alert to an Opportunity owner in day to Opportunity Closing date , or this class can be reused for any objects with custom message with minimum required re coding. I have in Included Test Class too .
Before you send a custom notification, you must first create a notification type:
How to create a Notification Type :
To send custom notifications via a process in Process Builder, a Flow in Flow Builder, or invocable action API you must Create notification types.
Supported REST HTTP Methods
* This object is available in API version 46.0 and later.
- URI
- /vXX.X/actions/standard/customNotificationAction
- Formats
- JSON, XML
- HTTP Methods
- GET, HEAD, POST
- Authentication
- Authorization: Bearer token
Inputs
Input Details customNotifTypeId - The ID of the Custom Notification Type being used for the notification.
recipientIds - The ID of the recipient or recipient type of the notification.
- UserId — The notification will be sent to this user, if this user is active.
- Values can be combined in a list up to the maximum of 500 values.
senderId - The User ID of the sender of the notification.
title - The title of the notification, as it will be seen by recipients. Maximum characters: 250.
body - body of the notification, as it will be seen by recipients. Maximum characters: 750.
targetId - The Record ID for the target record of the notification.
Sample Output:
Class CustNotify will get Notification Type Id, Generate JSON and call REST API to Trigger Bulk/single Notification for Object Owner !!
- String NotifTypeID = [SELECT Id FROM CustomNotificationType WHERE DeveloperName = :NotifiDeveloperName].Id;
above SOQL will get Notification Type Id by passing Notification Developer Name.
- Here is the Code Example : and this Schedule Class is to Execute CustNotify !
- And my use case was to execute Every Midnight to schedule to Notify Opportunity Owners .
System.schedule('Scheduled CustNotify', '0 0 0 * * ?', new CustNotifyScheduledCron ());
Very helpful content for SF developers. Thanks for sharing.
ReplyDeleteThanks for sharing Zakir.
ReplyDeleteShall we try this without target id.I would like to send a notifications to admin users about my appexchange app release info.
also, is body field is supported to html body?
Please help me.
Hi Ramugadu,
DeleteThanks for your findings .. However your requirements might not support right now but once I have any outcomes I’ll let you know.
targetId is a Required. The Record ID for the target record of the notification.
And the notification not supported html content.
Your can send email notification for your Use Case.