About Me

My photo
Dhaka, Dhaka, Bangladesh
✔4x Salesforce Certified ✔Web Application Developer ✔Database Developer with DWH/ETL/BI • Successful solution engineer and developer with 16+ years of experience multiple technology and in different countries. Proficient in implementing business requirements into the technical solution. Experience handling all phases of the project lifecycle from discovery through to deployment. Worked as technical lead for a team of junior developers to make sure code stayed in line with requirements and standard best practices. Skilled at integrating disparate systems with Salesforce.Experience implementing Salesforce Community Cloud at two previous companies.

Tuesday, April 14, 2020

Implement Salesforce Custom Notification Actions using Apex code Rest API Included Test Class

‪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.
  1. Enter Notification Builder in the Quick Find box in Setup, then select Custom Notifications.
  2. Click New and add your Custom Notification Name and API Name, and supported channels.
  3. Save your notification type.
  4. If you enable the mobile channel, you must enable the supported apps for your notification type.
    1. From Setup, enter Notification Builder in the Quick Find box, then select Notification Delivery Settings.
    2. Choose your custom notification type, and select Edit from the dropdown menu.
    3. Select the supported applications for your notification type and save.
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


InputDetails
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 ());
 

Here is the Test Class


 
Ref: 
https://developer.salesforce.com/docs/atlas.en-us.api_action.meta/api_action/actions_obj_custom_notification.htm
https://help.salesforce.com/articleView?id=notif_builder_custom_type.htm&type=5



3 comments:

  1. Very helpful content for SF developers. Thanks for sharing.

    ReplyDelete
  2. Thanks for sharing Zakir.

    Shall 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.

    ReplyDelete
    Replies
    1. Hi Ramugadu,
      Thanks 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.

      Delete