Forcetrails: Send Custom Notification using apex | Salesforce | Messaging.CustomNotification

Hello Friends!!

In this post, we are going to have a sneak peek of the new feature in the Apex programming language. In Winter 2021, salesforce is releasing this new feature. I have tried this in the release preview org so wanted to share the sample code.

Basically, you will be able to send custom notifications with the apex after this release.

winter-2021-send-custom-notification-using-apex-min


What is the custom notification?

Custom notifications are customized notification inside salesforce org. The admins can send this notification when some important event occurs. For example, the opportunity owner is notified when the opportunity is approved by his manager. Or another example is like the account owner is notified when the new case is logged.


Where we get these custom notifications?

Custom notification is shown on the bell icon inside the salesforce org. See the below image.

send-custom-notification-using-apex-min

Also in the salesforce mobile app, custom notifications are shown as in-app notifications.


How to send custom notification using apex?

Let's see this step by step.

Create a custom notification type

  1. Type 'Notifications' in the setup search.
  2. Click 'Custom Notifications' under Custom Notifications.
  3. Click the 'New' button from the right-hand side.
  4. Enter your desired custom notification name and API name.
    Here I have chosen 'MyCustomNotification'.
  5. Select the channels (Mobile and/or Desktop).
    Here I have selected both as I want the notification to be shown on both mobile and desktop devices.


Send the custom notification using the below code

Messaging.CustomNotification notification = new Messaging.CustomNotification();
notification.setBody('This is body of the custom notification!');
notification.setTitle('Hi this is first notification sent using apex!');
notification.setSenderId(Userinfo.getUserId());
CustomNotificationType type = [SELECT Id FROM CustomNotificationType WHERE DeveloperName = 'MyCustomNotification'];
notification.setNotificationTypeId(type.id);
notification.setTargetId('006B0000005hCxzIAE'); // target object id
notification.send(new Set<String> { Userinfo.getUserId() });


Let's see the breakdown of the above code.

  • Firstly we have queries the custom notification type that we created in the first step of this section.

    SELECT Id FROM CUstomNotificationType WHERE DeveloperName = 'MyCustomNotification'
    
  • Messaging.CustomNotification is the new class introduced with this feature, which we use to create and send the custom notification? This class contains the methods to set the different properties of the custom notification.
  • The setBody, setTitle methods are used to set the contents of the notification. These are required.
  • setSenderId is the Id of a user who is sending the notification. This is optional.
  • setNotificationTypeId sets the reference to CustomNotificationType object. This is required.
  • setTargetId the method sets the target record of the notification. On click of the notification, the user is redirected to the record id set here. This is required.
  • send the method is used to send the notification, you must populate all required data before calling this method.


Conclusion

Now you won't need to do fancy workarounds to send custom notification using apex. Before this release, there were multiple ways to do this using apex which were not so sophisticated and easy to implement.


References

Other ways to send custom notifications

5 comments:
  1. How to write test class for the Custom notification from apex

    ReplyDelete
    Replies
    1. Hello Mr./Ms Unknown!!

      Simply call the method which is sending the notification in your test method. This code runs just fine in the test environment. But note that you can not assert if the notification is sent or not.

      Delete
  2. Hi Rahul,
    I am getting this error , if i call the method .

    Error : Methods defined as TestMethod do not support Web service callouts.

    Please help how to resolve this.

    Thanks..!

    ReplyDelete
    Replies
    1. Hi manikanta, this error has nothing to do with CustomNotification. please check if there is any callout in your code. You might need to write mock class for that.

      Delete
  3. Rahul, any example to send notification to a Lightning record page or Component with parameters? Basically using 'pageReference' option.

    ReplyDelete

Hi there, comments on this site are moderated, spams and promotions will be deleted. If you have any concern, or if you are not able to comment for some reason, reach email us at rahul@forcetrails.com