Blog

Salesforce Platform Events

In 2017 Salesforce introduced platform events to support event-driven architecture.  Companies want a connected ecosystem.  Before platform events, it required custom point-to-point integrations.  Platform events represent a cheaper, faster and better way of integrating with Salesforce.

What are Platform Events?

Platform events are messages that are published and subscribed to. One or more subscribers can listen to the same event and carry out independent actions. Platform events provide a way to decouple the key components of an integration, which reduces codebase and complexity.  

Salesforce Platform Event ArchitectureThe main components are the pipe, window, and event:

The pipe is the message bus where events are placed in chronological order.  The window represents the period of time.  There are two types of eventsPublishers are apps that send events to the pipe. Consumers subscribe to events.

Platform Events In Action

Create Platform Event

If you have created a custom object, then to create a Platform Event is similar.   Navigate to the setup, in quick find type Platform Events, and select new platform event.  See sample Contract Event.  Note the API Name suffix __e instead of __c for the typical custom object.

Create Platform Event Object

I created a contract event to capture contract activations.  I added some fields:

– Status

– Account Number

– Contract Number

Note:  Published events cannot be edited/deleted.  To consume the event, an app can subscribe to it (as opposed to querying it).

Publish Platform Event

In this example I want Salesforce to publish an event when a contract is activated. With the event configured, we need a trigger on the standard contract object to publish the event.

Publish Platform Event

This logic fires when the contract’s status changes to activated.  Note the EventBus.publish()method that pushes the messages to the pipe.  In the Salesforce APEX developer guide, it notes the following:

  • 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.

Subscribe to Platform Event

Entities that consume platform events should subscribe to the event’s channel.  External systems can subscribe to platform events using CometD. CometD is the highly scalable HTTP-based event-routing bus.

Subscribing to platform event notifications is like subscribing to push topics. The platform event for my contract event is as follows:

/event/Contract_Event__e

Here is an example of how the contract event message displays when received by the external system.

Subscribe to Platform Event

The external system receives notification when the contract activates. The message contains the payload and replay ID.  The replay ID is a system generated value, and it is used to position the event in the event stream.

Reversing The Scenario

If the external system needs to pass information back to the contract, it could be the publisher and Salesforce the subscriber.  External systems can publish to Salesforce using the REST APIs. Publishing events work like inserting sObject records. Using the REST API the endpoint looks like:

/services/data/v40.0/sobjects/Contract_Event__e/

The request body would is as follows:

Salesforce could subscribe to this event when status is canceled and update the related contract record.  This scenario illustrates how one platform event can be used by multiple subscribers to accomplish different actions.

Platform Events Recap

There is a lot to love about Platform events.  It is a great alternative to complex end-to-end integration. Since the publish and subscribe processes are independent of one another, they are more durable, flexible and easier to maintain.

Platform events represent a real force multiplier when an integration involves many entities.  For example, imagine one published contract event with many subscribers all performing distinct processes.  In our use case above, the external system subscribed to the activated contract.  That same contract event could trigger multiple actions such as:

– Onboard a customer

– Provision services

– Submit customer notifications

Complex scenarios can be decomposed into separate actions, which are triggered by the same platform event.  With Platform Events, we no longer need to build a custom point-to-point connection with its own custom logic to address each scenario.

Salesforce Platform Events Breakdown