Newer Version Available
Invoke the Publish Callback
First, create an instance of the callback class. For example, we use the FailureCallback class that we implemented earlier.
1FailureCallback cb = new FailureCallback();This publish call publishes a list of events and passes in a callback instance.
1List<Database.SaveResult> results = EventBus.publish(eventList, cb);This publish call publishes one event and passes in a callback instance.
1Database.SaveResult sr = EventBus.publish(myEvent, cb);Callback Status Code
When you publish an event with a callback instance of EventBus.EventPublishFailureCallback or EventBus.EventPublishSuccessCallback and the publish call is successful, the returned Database.SaveResult contains a status code of OPERATION_WITH_CALLBACK_ENQUEUED in the StatusCode field of Database.Error. Also, the event universally unique identifier (UUID) is returned in the Message field.
1Database.SaveResult[getErrors=(
2 Database.Error[
3 getFields=();
4 getMessage=d473406e-0922-432a-9088-b8c95ef8b548;
5 getStatusCode=OPERATION_WITH_CALLBACK_ENQUEUED;]
6 );
7 getId=e02xx0000000001AAA;
8 isSuccess=true;]If the Apex publish callbacks feature is disabled in your Salesforce org, the EventBus.publish calls that use callbacks still execute but don’t invoke the callbacks. Also, the returned status code is OPERATION_ENQUEUED.