Salesforce Platform Events

Hello Trailblazers,

In this article we will discuss about the platform events. This covers basic idea of Platform Events, how platform events work, how to use Platform Events and some considerations for using Platform Events.

Salesforce Platform Events follow Event Driven Architecture. So what is event driven architecture? In simple words, whenever any kind of action happens you notify someone with some message, and we call that action as an Event, message that it carries is known as Event Message, someone who is going to receive that message is known as Event Consumer, environment where action has happened is Event Producer and common channel through which Producer sends an event and receiver reads message is known as Event Bus.



This is completely different from request-response structure, your system/application just have to subscribe to event channel and they will be notified in real-time whenever event occurs. Receivers are always in listening mode and more than one receiver can subscribe for single event. But receivers and producers are not dependent on each other.

The Salesforce enterprise messaging platform uses this architecture. Publishers and subscribers communicate with each other through platform events. This does not require any complex processing. 

Platform events in Salesforce are similar to custom objects. We create fields which carry data that needs to be sent to receiver. Platform event API name is appended by __e.

There are two types of Platform Events-

  1. Standard Platform Events: These are the standard system events with standard fields.
  2. High-Volume Platform Events: These are custom events that we will be creating. These type of events are used to process millions of events efficiently and to scale your event-based apps.

We can’t create new event definitions as Standard Platform Events, they are High-Volume Event by default.

We can create Platform event through setup just like custom object.

Publish Behavior has two values –

  1. Publish After Commit: Event is published only after a transaction commits successfully. allOrNone header value is considered here, so allOrNone is true and even if single record fails then all events are not published.
  2.  Publish Immediately: Events are publish immediately whenever publish call is executed. allOrNone header value is completely ignored here. You can not use even setSavepoint and rollback methods here.

After this you can create custom fields to hold data while publishing an event.

Platform Events has ReplayId system field, this field is unique for each event and is populated when that particular event is delivered to receivers.

 

How to create platform event records?

We do not get any tab or any salesforce user interface to create/publish platform events. But we can publish platform events through API and Declarative way.

Publish Platform Events using Process – Declarative Approach

  1. Create process as per your business requirement.
  2. Add Create Record action to your process. Select Platform Event Name as Record Type.
  3. You get all the fields of event there to set values. Add mappings as per your requirement and save action. That’s it, event will be published whenever your process is executed.

Publish Platform Events using Flow – Declarative Approach

  1. Create flow as per your requirements.
  2. Add Create Records Element.
  3. For How many records, select one.
  4. For How To Set Record Fields, select Use separate variables, resources, and literal values.
  5. For object, select Platform Event name.
  6. Set field values as per business mappings and Save.

Publish Platform Events using Apex

To publish events from apex code, we use EventBus.publish method.

Publish Platform Events using API

REST API:

To publish a platform event message using REST API, send a POST request to the following endpoint, with JSON of fieldname and value.

/services/data/v48.0/sobjects/Example_Event__e/

 

SOAP API:

To publish a platform event message using SOAP API, use the create() call.

Consideration while using platform event

  • We can’t query Platform Events through SOQL/SOSL.
  • Published events can’t be rolled back.
  • We can’t view event records in the Salesforce User Interface.
  • High-volume platform event messages are stored for 72 hours (3 days).
  • Standard-volume platform event messages are stored for 24 hours (1 day).
  • Salesforce assigns a replay ID value to a received platform event message and persists it in the event bus.
  • Only after triggers are supported.


Comments

Post a Comment

Popular posts from this blog

Salesforce Flow - Create multiple records from user input

Scan barcodes from Salesforce Mobile App

Zip or Unzip Files in Salesforce