EventBus Class

Contains methods for publishing platform events.

Namespace

System

EventBus Methods

The following are methods for EventBus. All methods are static.

getOperationId(result)

Returns the ID of the asynchronous event publishing operation based on the passed-in SaveResult. Use this ID to correlate the asynchronous publishing result sent on the /event/AsyncOperationEvent channel.

Signature

public static String getOperationId(Object result)

Parameters

result
Type: Object
The SaveResult that is returned by the EventBus.publish call.

Return Value

Type: String

publish(event)

Publishes the given platform event.

Signature

public static Database.SaveResult publish(SObject event)

Parameters

event
Type: SObject
An instance of a platform event. For example, an instance of MyEvent__e. You must first define your platform event object in your org.

Return Value

Type: Database.SaveResult

The result of publishing the given event. Database.SaveResult contains information about whether the operation was successful and the errors encountered. If the isSuccess() method returns true, the event was published for a standard-volume event. For a high-volume event, the publish request is queued in Salesforce and the event message might not be published immediately. For more information, see High-Volume Platform Event Persistence. If isSuccess() returns false, the event publish operation resulted in errors, which are returned in the Database.Error object. This method doesn’t throw an exception due to an unsuccessful publish operation.

Database.SaveResult also contains the Id system field. The Id field value is not included in the event message delivered to subscribers. It is not used to identify an event message, and is not always unique.

Usage

  • The platform event message is published either immediately or after a transaction is committed, depending on the publish behavior you set in the platform event definition. For more information, see Platform Event Fields in the Platform Events Developer Guide.
  • Apex governor limits apply, including DML limits. Each method execution is counted as one DML statement.

publish(events)

Publishes the given list of platform events.

Signature

public static List<Database.SaveResult> publish(List<SObject> events)

Parameters

events
Type: List<sObject>
A list of platform event instances. For example, a list of MyEvent__e objects. You must first define your platform event object in your org.

Return Value

Type: List<Database.SaveResult>

A list of results, each corresponding to the result of publishing one event. For each event, Database.SaveResult contains information about whether the operation was successful and the errors encountered. If the isSuccess() method returns true, the event was published for a standard-volume event. For a high-volume event, the publish request is queued in Salesforce and the event message might not be published immediately. For more information, see High-Volume Platform Event Persistence. If isSuccess() returns false, the event publish operation resulted in errors which are returned in the Database.Error object. EventBus.publish() can publish some passed-in events, even when other events can’t be published due to errors. The EventBus.publish() method doesn’t throw exceptions caused by an unsuccessful publish operation. It is similar in behavior to the Apex Database.insert method when called with the partial success option.

Database.SaveResult also contains the Id system field. The Id field value is not included in the event message delivered to subscribers. It is not used to identify an event message, and is not always unique.

Usage

  • The platform event message is published either immediately or after a transaction is committed, depending on the publish behavior you set in the platform event definition. For more information, see Platform Event Fields in the Platform Events Developer Guide.
  • Apex governor limits apply, including DML limits. Each method execution is counted as one DML statement.