Platform Events Developer Guide
Get the Result of Asynchronous Platform Event Publishing with Apex Publish Callbacks
Event publishing is asynchronous, and the immediate result returned in SaveResult is the result of queuing the publish operation in Salesforce. Sometimes immediate errors are returned, for example, due to a missing required field in the event message. If no immediate errors are returned and when resources become available, the system carries out the queued publish call, and the final result is sent in an Apex publish callback.
Apex publish callbacks are available for high-volume platform events. Legacy standard-volume events aren’t supported.
Note
To walk through the steps of creating and using Apex publish callbacks, see "Get Asynchronous Publish Results with Apex Publish Callbacks" in the Platform Events Debugging Trailhead module.
Note
-
Apex Publish Callback Class
An Apex publish callback contains the result of an asynchronous publish operation in Apex. After the publish operation completes and the final result is ready, the system returns a callback. You can implement one of these two interfaces: EventBus.EventPublishFailureCallback for failed publishes and EventBus.EventPublishSuccessCallback for successful publishes. -
Callback Running User and Debug Logs
A publish callback runs under the Automated Process user. As a result, all records that are created in a callback have their system user fields, such as CreatedById and OwnerId, set to Automated Process. -
Create an Event with an EventUuid Field
The EventUuid field uniquely identifies an event message and is used to match the events returned in the callback result with the events in the publish call. To have the system generate an EventUuid field value in each event object, use the SObjectType.newSObject(recordTypeId, loadDefaults) Apex method to create the event object. -
Invoke the Publish Callback
To have the system invoke the callback when the final publish result is available, pass in an instance of the callback class as the second parameter in the EventBus.publish call. -
Publish Callback Best Practices
Keep in mind these best practices for publish callbacks when implementing this feature. -
Example: Publish Callback Class That Creates Follow-Up Tasks for Failed Publishes
This publish callback class creates a task when event publishing fails in the onFailure method. The inserted task includes the number of failed events and the event UUIDs. -
Example: Publish Events with a Callback Instance
To invoke the callback, perform an EventBus.publish call by passing it an instance of the FailureCallback class. You can publish one event or a batch of events with the callback. -
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. -
Example: Publish Callback Class That Correlates Callback Results with Event Messages
This example callback class implementation shows how to retry publishing failed events. It’s based on a trigger on the Order object. -
Test Apex Publish Callbacks
To test your Apex publish callback class, add an Apex test class. You must provide Apex tests before you can package or deploy an Apex class to production and meet code coverage requirements. -
Apex Publish Callback Limits
Keep in mind this limit for Apex publish callbacks.