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
  • A status change in Salesforce notifies external order fulfillment apps of a product shipment order.
  • An external product app notifies Salesforce of merchandise returns

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

Characteristics

A platform event is a special kind of Salesforce entity, similar in many ways to an sObject. An event message is an instance of a platform event, similar to how a record is an instance of a custom object. Unlike with custom objects, you can’t update or delete event records, or view event records in the Salesforce user interface.

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 CloudFountain wants to notify its subscribers of its product launches. We’ll create a Platform event object called CloudFountain and a text field named Description to store the message.

When you create a platform event, the system appends the __e suffix to create the API name of the event. In our example, for the CloudFountain event, the API name is Cloud_Fountain__e. The description field still is as usual Description__c

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.

Publish Through Apex

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

Publish Through API

Endpoint to call

/services/data/v40.0/sobjects/Cloud_Fountain__e

JSON Request Body

Response

When you publish platform events, DML limits and other Apex governor limits apply.

Subscribe to Platform Event Notifications with Apex Triggers
With platform events, the process is similar to database triggers. You simply write an after insert Apex trigger on the event object Cloud_Fountain__e to subscribe to incoming events. Triggers provide an auto subscription mechanism in Apex. Triggers receive event notifications from various sources whether they’re published through Apex or APIs. We will see this in “How & What: Salesforce Platform Events (Part 2)”

Platform events support only after insert triggers. The after insert trigger event corresponds to the time after a platform event is published. After an event message is published, the after insert trigger is fired.

As always,  @CloudFountain, your feedback would be invaluable. Please feel free to contact us in case of any questions or concerns.

Related posts