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.
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.
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
- Type 'Notifications' in the setup search.
- Click 'Custom Notifications' under Custom Notifications.
- Click the 'New' button from the right-hand side.
-
Enter your desired custom notification name and API name.
Here I have chosen 'MyCustomNotification'. -
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.
How to write test class for the Custom notification from apex
ReplyDeleteHello Mr./Ms Unknown!!
DeleteSimply 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.
Hi Rahul Gawale,
DeleteThanks 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..!
Hi LOCATA,
DeleteI 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.
Hi Rahul,
ReplyDeleteI 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..!
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.
DeleteRahul, any example to send notification to a Lightning record page or Component with parameters? Basically using 'pageReference' option.
ReplyDeletethis 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?
DeleteHi ,
ReplyDeleteHow to include merge function in the body of the notification
Hi Nithya, you need to merge the feilds in the text bebore putting it into notification body.
DeleteWhat 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?
ReplyDeleteHi Muyeen! You can send the same notification to multiple users by providing list of user ids. For limitations see this post.
DeleteI 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?
DeleteHi Muyeen, I think as of now you can't do that. You need to call it multiple times.
DeleteHi Rahul,
ReplyDeleteIs there any limitation around the send method like below,
1. In a transaction how many times we can call send() method
2. Can we call this send() method via future, batch , queueable apex
Thank You,
Rajesh Adiga P.
Hi Rajesh, yes there are some limitations, like 500 types of notifications and 10000 notifications per hour per org. Here you can find more details on that Considerations for Processes that Send Custom Notifications
DeleteHey, do you know if is possible to open a list view on notification clicking?
ReplyDeleteI think yes if you provide the relative URL of the list view as a link in the notification, but I haven't tried yet.
Delete