Newer Version Available
Publish Callback Best Practices
Don’t Republish the Same Event Object That Is Created with SObjectType.newSObject
If you create an event object with the SObjectType.newSObject(recordTypeId, loadDefaults) Apex method, we recommend that you don’t publish the same event object more than once. Because the event object is populated with an EventUuid value, if you publish it more than once, non-unique EventUuid values are tracked in the callbacks. The duplicate EventUuid values can cause unexpected results. This behavior doesn’t apply to events that you create using the API name Event_Name__e event = new Event_Name__e().
Publish a List of Events Instead of Individual Events with a Callback
When using a callback in an EventBus.publish call and you want to publish several events, we recommend that you create a list of events and publish the events in one EventBus.publish call. Using one EventBus.publish call for all events is more efficient than making a call for each event because it uses less Apex governor limits for the publish call. Also, the system attempts to batch callback executions for a list of events.
This example creates a list of events and then passes it through the events variable to the EventBus.publish call. This snippet results in one call to the publish method with a callback instance.
In contrast, this example shows what to avoid. It’s inefficiently making 10 calls to the publish method with a callback, each with one event. This example can result in more callback executions later than when events are batched in one publish call.
Publish a List of Events with a Callback with a Platform Event Type
If you create events using the API name, you can publish a list of events with a callback only if you define the list with the specific platform event type. The generic SObject type isn’t supported. For example, you can define a list of events as:
But not as:
Then you can publish the events with a callback.