Example: Publish Callback Class That Creates Follow-Up Tasks for Failed and Successful Publishes
This publish callback class is a modification of the previous example—it also implements the EventBus.EventPublishSuccessCallback interface and processes both success and failure cases. It creates a task when event publishing fails or succeeds. The inserted task includes the number of failed events and the event UUIDs.
Before running this example, change the email address in the example to an email address of a user who has permission to create tasks in your org. To view debug logs for the FailureCallback class, make sure that you set up user trace flags for the Automated Process user. For more information, see Callback Running User and Debug Logs.
To publish events with the callback class, use the code snippet in Example: Publish Events with a Callback Instance and change the callback instance name in the EventBus.publish method to FailureAndSuccessCallback.