How To Send Custom Notifications From Lightning Flow

 Introduction:

Salesforce Custom Notifications is yet another cool and useful feature that was introduced in Summer ’19 release. This is a new way to keep your users in the know when important events occur. You can send push notifications on mobile or desktop alerts when a record is created or edited and any specific condition is met.

What we'll going to learn:

  • How to create a New Custom Notification in Salesforce?
  • How to create a record triggered flow on Task object.
  • How to send Custom Notification from lightning flow?
  • How to get Id of Custom Notification Type in Flows?
  • How to notify Task owners on insertion of new Task records?

 What are Custom Notifications: 

Salesforce Custom Notifications is actually a better alternative to sending emails to users every time certain conditions are met. For e.g. If a Case is created for an Account then sending a custom notification to Account owner with Case details is better than sending an email. It is beneficial because once user clicks on the notification, he/she can directly access that particular record. Also, there is less switching between the apps.

Why Custom Notifications:

  • As we know, nobody likes to be spammed with 100s of Emails daily. And also there is a very huge chance that users miss some of them or don't even bother to see Email notifications messages. Also, if we use email notifications, we need to jump screens from Email Inbox/Client to Salesforce which is not always desirable. 
  • Custom Notifications are user friendly and screen friendly, no need to switch Apps and everything resides within Salesforce Screen. No Screen Refresh Required as this notification appears automatically on any current Salesforce Page that you are logged in. 
  •  If using Salesforce Mobile App, it can be used to show Push Notifications on Mobile Screens similar to other apps like Twitter, Instagram etc.
 

 Problem Statement:

Let's assume a scenario, John Smith is a Sales Head at Universal Containers(After this referred as UC). At UC they have a mobile application from which the end customer can send messages  to the Sales Representative and in Salesforce we are storing them as Task records who's Type field value is "App Message". 
So John wants that whenever a new Task record is getting created who's Type field has the value "App Message" then send a Custom Notification to the owner of the Task record with the following details:

Notification Body = {!Task.Subject}.
Notification Title = App Message From {!Task.Account.Name}.
Recipient Ids = {!Task.OwnerId}.
 

Solution Statement:

Understanding the requirement from John Smith, Rebecca Black who is UC's Application Architect suggested the solution in the form of a record triggered lightning flow for sending the custom notifications whenever a new task record is getting created. we can use a Flow Core Action : Send Custom Notification. We will see later in the post how to use this.

Now we'll going to build the solution in steps.

Step 1, Creating Custom Notification Type:

  • Go to Setup > Notification Builder > Custom Notifications.
  • Click New.
  • Provide a Name and desired Channels and Save the record.

Custom Notification: 

Label: New App Message Task.

Name: NewAppMessageTask.

Supported Channels: Desktop & Phone.

Step 2, Adding "App Message" picklist value to Type picklist in Task object:


Step 3, Create a Record Triggered flow on Task object:

  • Go to Setup > Process Automation > Flows.
  • Click New and choose "Record -Triggered Flow".
  • Choose object name "Task" and select "A record is created" option.
  • Set the entry criteria "Type equals App Message AND WhatId not null" as shown below in the image.
  • Choose "Actions and Related Records" option because we want to send custom notification after the Task record is created.


  •  Click Done.

Now before we move forward, let's discuss how to use Send Custom Notification Core Action in Flows. 

This is an Out of Box Core Action Available for use in the Flows. It has many Input Values that are required in order to configure and Use it. Let's see how to use these values and where to get their Data from.

The Required Input Values are as shown in image below:
 

 
The most Important and Tricky one is Custom Notification Type ID. Let's first see about the Custom Notification Type Id.
 
There is a small gap in Salesforce Custom Notification Builder Functionality. The Problem is that we cannot find out directly the Id of the Custom Notification Record. And it's a problem because this ID is required in Send Custom Notification Action from Flows or any other place in Salesforce. 
 
In Flows we have to query the object named "Custom Notification Type" to get the Id and the filter condition will be DeveloperName = API Name of the Custom Notification Type(i.e. NewAppMessageTask).
 
  •  Now Drag and Drop or Add GetRecords Element to Flow Canvas.
  • Add label as "Get Custom Notification Id" and API name as "GetCustomNotificationId".
  • Select "Custom Notification Type" as object name.
  • Add the filter condition: DeveloperName =  NewAppMessageTask.
 Important : As we need only one Id and it is a single record, just use the option "Get only the First Record" in Get Element. Checkout below image. Since we are querying on the object, we will get all the Field Values (including Custom Notification Id) of this Record after the Get Record Element and all the Field values will be stored in the Variable "Get Custom Notification Id" which is the Name of our Get Element.
 


So, till now we have managed to get the Id of the Custom Notification we created within the Flow. Now we need some other values to provide to the Send Notification Flow Core Action. First we will see about the Recipients Ids which is also a required Field in this Action.

  • Since we can provide Recipients ID value in Flow Action : Send Custom Notification only by using a Variable (Collection Variable) and Assigning a value to that Variable using Assignment Element and not through hard-coding the value into the Action directly. 
  • Create New Collection Variable of Type Text to hold Recipient Ids. (Click on Allow multiple values(collection).
 

  • Use Assignment Element to add Ids to Recipient Ids Collection Variable created. We can use "Add" operator in Assignment Element to add multiple values of same or different types. The variable name is "RecepientIds" {!RecepientIds} as we created in Last Step. I am just using one row of Add operator just to assign the value of the Task Records's Owner Id.

 
So till now we have got Custom Notification Id and Recipients Ids which we need to provide in the Flow Action as we saw earlier.

But there are Two more Required Fields left as per the image I shared earlier. They are Notification Title and Notification Body. 

We can directly hard-code or provide a message directly in these fields but I am creating a new Formula Type Variable to show a title with related Account Name which will be dynamically populated based on the Name of the Parent Account Record of the Task record that fired this Record Triggered Flow.
 
 
  • For Notification Body, we'll going to use Subject field of the Task record.
  • Finally, Let's Configure the Flow Action : Send Custom Notification, Also, apart from this we will provide one more value "Target ID" in this action.This will be just the Record Id of the Email Message that Fired this Flow {!Record.Id} to redirect to the record detail page when user clicks on the custom notification message.

 
That's it! Save the Flow and Activate it.
 
 

Now whenever an Task is received on a Account, Task owner will be notified through a notification on the Bell Icon. 


This is a very basic example to show this capability. This powerful feature can be used in many ways to deliver custom notification messages to Users in many different scenarios.

Hope this helps! See you next time!

Happy Coding... 😊


Post a Comment

Previous Post Next Post