Pages

Thursday, June 6, 2019

Fire Platform Events from Batch Apex using Database.RaisesPlatformEvents Interface

With Summer'19 release (API version 44 or later), now it is possible to fire platform events from batch apex. So whenever any error or exception occurs, you can fire platform events which can be handled by different subscriber.

Batch class needs to implement "Database.RaisesPlatformEvents" interface in order to fire platform event.

global class SK_AccountProcessBatch implements Database.Batchable<sObject>,Database.RaisesPlatformEvents{
   //batch logic
}

To understand more about platform events please refer below links

Platform Events : Way to Deliver Custom Notifications within Salesforce or to external Application

Here I will be writing simple batch class which process account records and if any errors occurs during update, then all account record Ids will be published using platform event.

I have created platform event with below mentioned fields:


Below is batch apex code which will fire platform event and I wrote a trigger which will subscribe to platform event.

I have used Database.Stateful interface to store the record ids which are getting failed in each batch execution. In finish method, I am firing platform event with all failed records Ids.

Below is logs generated by trigger which will get executed whenever platform event is fired.


In order to see debug logs for platform events subscription, add a trace flag entry for the Automated Process entity in Setup. The debug logs aren’t available in the Developer Console’s Log tab.

Navigate to SetUp --> Debug Logs --> New
  • For Traced Entity Type, select Automated Process.
  • Select the time period to collect logs and the debug level.
  • Click Save.
I have also created a lightning component which will subscribe to platform events and will display the event message as shown below in snapshot:

Please refer below link to understand how to use platform events in lightning components:

Handling Platform Events in Lightning Components


Hope this will help!!!


13 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Nice Blog ! If you are facing issues with QuickBooks and need help,dial our toll-free number Accounting Services 1-800-986-4607 without any delay. Get 100% reliable solutions and perform accounting tasks smoothly. We have a toll-free number 1-800-986-4607 on which you can reach anytime you wish.

    ReplyDelete
  3. This is Good information about this topic..I like it.. wordpress database fix ..Keep it Up!

    ReplyDelete
  4. I love the way you write and share your niche! Very interesting and different! Keep it coming! Fire alarm systems

    ReplyDelete
  5. I think about it is most required for making more on this get engaged יועץ בטיחות כיבוי אש

    ReplyDelete
  6. Well, this got me thinking what other workouts are good for those of us who find ourselves on the road or have limited equipment options. industri judi

    ReplyDelete
  7. That is really nice to hear. thank you for the update and good luck. מערכת גילוי אש מחיר

    ReplyDelete
  8. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article. Vacuum casting ABS PC Pom parts

    ReplyDelete
  9. This information about fire is very helpful for us and through this, we can get many benefits. Many people liked this content because it is beneficial for us. Coursework writing services.

    ReplyDelete
  10. This is the most stable release till now. We integrated it in our systems which are used for the best live video monitoring service and it worked just fine, the way we wanted.

    ReplyDelete
  11. Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for.wireless fire alarm system

    ReplyDelete