Salesforce Platform Events

Salesforce Platform Events

Platform Events

Platform Events are based on Event-Driven Architecture(EDA). EDA is a framework to define the production, detection, consumption of, and reaction to events. The two components of EDA are event creators and event consumers. Event creators responsibility is to just create the event and Event consumers responsibility is to know when the event occurs and take an action. In EDA, no requests need to be made to a server to obtain information about a certain state. Instead, a system subscribes to an event channel and is notified whenever new states occur. Any number of consumers can receive and react to the same events in near real time.

Use Cases

Salesforce and external systems communicate through platform event messages.

News agency sends events to subscribed clients with the latest breaking news about traffic and road conditions in a mountain retreat destination

Use platform events in the following cases:

  • To send and receive custom event data with a predefined schema
  • To publish or subscribe to events in Apex
  • For the flexibility of publishing and processing events on and off the  Salesforce platform

Create Platform Events

From Setup, enter Platform Events in the Quick Find box, then select Platform Events.

On the Platform Events page, click New Platform Event.

You define the custom data that platform events contain. Just like custom objects, you define platform events in Salesforce. You create a platform event definition by giving it a name and adding custom fields.

Let’s say CloudNews wants to notify its subscribers of its product launches. We’ll create a Platform event object called CloudNews and a few fields to store the message.

Publish Platform Events

If your app is on the Salesforce platform, you can publish events using an Apex method or with declarative tools, such as Process Builder or the Cloud Flow Designer. If your app is an external app, you can publish events using Salesforce APIs.

To publish more than one event in the same call, add your events to a list of events, and pass the list to the EventBus.publish() method. The output of this method is an array of Database.SaveResult objects

Subscribe with Apex Triggers

Apex Triggers receive event notifications from various sources—whether they’re published through Apex or APIs. Platform events support only after insert triggers. After an event message is published, the after insert trigger is fired to listen to the news event and create a follow-up Case if the news event is an Urgent one.

Considerations while implementing platform events subscription:

  • There might be a delay between when an event is published and when the trigger processes the event since platform event trigger runs in its own process asynchronously.
  • A trigger processes platform event notifications sequentially in the order they’re received. An Apex trigger can receive a batch of events at once. The order of events is preserved within each batch.
  • Platform Events run under the Automated Process system user and not under the one who executed it.
  • System fields of records such as CreatedById and LastModifiedById also reference the Automated Process System user.

Platform Event triggers are subject to Apex governor limits and Apex Trigger Limits as usual.