Newer Version Available

This content describes an older version of this product. View Latest

EventBus Class

Contains methods for publishing platform events.

Namespace

System

EventBus Methods

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

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 event insertion occurs non-transactionally. As a result, you can’t roll back published events.
  • Apex governor limits apply, including DML limits. Each method execution is counted as one DML statement.

Note

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 event insertion occurs non-transactionally. As a result, you can’t roll back published events.
  • Apex governor limits apply, including DML limits. Each method execution is counted as one DML statement.

Note