Skip to main content

Platform Events in Batch Apex

Fire Platform Events from Batch Apex Classes

Introduction 

With winter 19 release you can able to fire the platform events from the batch apex.Batch Apex classes can opt in to fire platform events when encountering an error or exception. Clients listening on an event can obtain actionable information, such as how often the event failed and which records were in scope at the time of failure. Events are also fired for Salesforce Platform internal errors and other uncatchable Apex exceptions such as LimitExceptions, which are caused by reaching governor limits. An event record provides more granular error tracking than the Apex Jobs UI. It includes the record IDs being processed, exception type, exception message, and stack trace. You can also incorporate custom handling and retry logic for failures. You can invoke custom Apex logic from any trigger on this type of event, so Apex developers can build functionality like custom logging or automated retry handling. To fire a platform event, a batch Apex class declaration must implement the Database.RaisesPlatformEvents interface.

Step 1:  Create a Platform event

Here is the simple platform event object created for this example 

Step 2: Subscribe to platform event

I just created a simple trigger to subscribe for the platform events. During this beta release, Process Builder and flows do not support subscribing to these events.

Step 3: Raise Platform events

now you can raise the platform event from the batch apex.here is the simple batch apex that will raise the platform evens. After raising platform events all the subscribed channel will be receiving the events.
Go and execute batch apex
After raising the platform events, the subscribed trigger will create a case as shown below

Event Error Handling

The BatchApexErrorEvent object represents a platform event associated with a batch Apex class. This example creates a trigger to determine which accounts failed in the batch transaction. Custom field Dirty__c indicates that the account was one of a failing batch and ExceptionType__c indicates the exception that was encountered. JobScope and ExceptionType are fields in the BatchApexErrorEvent object.

Comments

Popular posts from this blog

Define & View Custom Big Object

Define a Custom Big Object: You can define custom big objects with Metadata API or in Setup. After you define and deploy a big object, you can view it or add fields in Setup. After you’ve deployed a big object, you can’t edit or delete the index. To change the index, start over with a new big object. To define a big object in Setup, see Salesforce Help. View a Custom Big Object in Setup After you’ve deployed your custom big object, you can view it by logging in to your organization and, from Setup, entering  Big Objects  in the  Quick Find  box, then selecting  Big Objects . Click the name of a big object, to see its fields and relationships. View a Custom Big Object in Setup After you’ve deployed your custom big object, you can view it by logging in to your organization and, from Setup, entering  Big Objects  in the  Quick Find  box, then selecting  Big Objects . Click the name of a big object, to see its fields and ...

Publish Paltform Events

Publishing Platform Events The Salesforce enterprise messaging platform offers the benefits of event-driven software architectures. Platform events are the event messages (or notifications) that your apps send and receive to take further action. Platform events simplify the process of communicating changes and responding to them without writing complex logic.  Publishers and subscribers communicate with each other through events.  One or more subscribers can listen to the same event and carry out actions. Here we will see how many ways you can able to publish the platform events in Salesforce.  After a platform event has been defined in your Salesforce org, publish event messages from a Salesforce app using processes, flows, or Apex or an external app using Salesforce APIs.Here is the simple platform event object we will be using in the examples here Here are the different ways you can able to publish the platform events in Salesforce Option 1: Using Proces...