Examples

Apex Platform Event

Publishing a Platform Event

Apex platform event publishes custom notifications with EventBus.

What is an Apex Platform Event?

Apex Platform Events allow you to publish and subscribe to custom notifications within Salesforce. They provide a scalable way to communicate changes in your data or processes across your Salesforce environment in real-time. Using EventBus, you can handle these events asynchronously, ensuring that your Salesforce applications remain responsive and efficient.

Creating a Platform Event

To begin using Platform Events, you must first define them in Salesforce. This involves creating a new Platform Event object, which is similar to creating a custom object. Each event is a schema with fields that define the data structure you want to pass with each event.

Publishing a Platform Event

Once your Platform Event is set up, you can publish events using Apex. Use the EventBus.publish() method to send events to the EventBus. Below is an example of how to publish a Platform Event:

Subscribing to Platform Events

To respond to published Platform Events, you can create Apex triggers. These triggers allow you to handle incoming events and define what actions should be taken when an event is received. Here's how you can subscribe to a Platform Event:

Use Cases for Platform Events

Platform Events are useful in various scenarios, such as:

  • Integrating Salesforce with external systems where real-time updates are crucial.
  • Decoupling complex processes by allowing different systems or components to react to events independently.
  • Implementing scalable, event-driven architectures within Salesforce.