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

14 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 Gawale,

      Thanks for your post. It really helpful.
      But I can't run it in Test Class. Test Class can't create or find this Custom Notification.
      Can you show me your test class.
      Thank you so much..!

      Delete
    3. Hi LOCATA,

      I have never written test class for this. Are you getting any error in the test class? I think you wont be able to verify in test if the message was sent or not. If you can specify the problem you are facing I am happy to help.

      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
    Replies
    1. this example will send a notification to a record page of the record whos Id is passed to the setTargetId method. i am not sure about the pagereference. Why you want to do so?

      Delete
  4. Hi ,
    How to include merge function in the body of the notification

    ReplyDelete
    Replies
    1. Hi Nithya, you need to merge the feilds in the text bebore putting it into notification body.

      Delete
  5. What is the limit to send the custom notifications? also if I have many custom notifications is there a way to put it in a list and send?

    ReplyDelete
    Replies
    1. Hi Muyeen! You can send the same notification to multiple users by providing list of user ids. For limitations see this post.

      Delete
    2. I understand we can provide the user Ids in the set, but I wanted to know how do I send different notifications using a list? is there a send function that processes list of notifications?

      Delete
    3. Hi Muyeen, I think as of now you can't do that. You need to call it multiple times.

      Delete

Hi there, comments on this site are moderated, you might need to wait until your comment is published. Spam and promotions will be deleted. We have moderated the comments for the safety of this website users. If you have any concern, or if you are not able to comment for some reason, reach email us at rahul@forcetrails.com