Send Custom Notification using Lightning Flow
As we know that custom notification can be send using process builder. Process builder will not work in below cases.
- To send a notification to admin about the batch class completion without sending e-mail
- To build some complex notification delivery using trigger.
- To send notification when APEX API is successfully executed.
We will build the use case to send notification to the Submitter when the batch job is complete
Step 1: Create a Auto Launch flow from flow builder and drag the action on the canvas.
Step 2: Configure the action by choosing the Notification Category as mentioned in below screenshot.
Step 3: Configure the input variables for each required value. Make sure that for the Recipient Id the variable type should of string collection.
Step 4: Create a Batch Class as below to send notification once batch class is successfully completed.
public with sharing class BatchLeadConvert implements Database.Batchable<SObject>{ private final String CONVERTED_STATUS = [SELECT Id, MasterLabel FROM LeadStatus WHERE IsConverted=true LIMIT 1].MasterLabel; public Database.QueryLocator start(Database.BatchableContext ctx){ return Database.getQueryLocator([SELECT Id FROM Lead WHERE ConvertedContactId = null]); } public void execute(Database.BatchableContext ctx, List<Lead> records){ //Execute any business Logic } public void finish(Database.BatchableContext ctx){ Map<String, Object> myMap = new Map<String, Object>(); List<String> reciepientIds = new List<String>(); AsyncApexJob a = [SELECT Id, Status, NumberOfErrors, JobItemsProcessed,TotalJobItems, CreatedBy.Id FROM AsyncApexJob WHERE Id =:ctx.getJobId()]; reciepientIds.add(a.CreatedBy.Id); myMap.put('notificationId', '0ML6F000000k9eCWAQ');// Id of your custom notification. You can store this in custom label. myMap.put('notificationBody', 'Batch class BatchLeadConvert is completed'); myMap.put('targetId', a.CreatedBy.Id);// target id can be of any type but here i am using the user id of the batch submitted by myMap.put('NotificationTitle', 'Batch Class is Completed'); myMap.put('recipientIds',reciepientIds); // list of recipeint Ids; Flow.Interview.Notification_Flow myFlow = new Flow.Interview.Notification_Flow(myMap); //Notification_Flow is name of your flow myFlow.start(); } }
Step 5: Once the batch class is completed a notification will be displayed in to bell icon.
Comments
Post a Comment