In Salesforce we use platform events to connect business processes within the salesforce and the external apps through exchange of real-time event data. Platform Events are secure and scalable messages that contain data.
Platform Events is based on EVENT-DRIVEN-ARCHITECTURE that enables an application to detect “Events” or important business moments and act on them in real time. Publishers publish event messages that subscribers receive in real time.
Salesforce event-driven architecture is consisting of
- Event Producers
- Event Consumers
A change in state that is meaningful in a business process. For instance, a placement of an order is a meaningful event because the order fulfillment center requires notification to process the order.
A message that contains data about the event. Also known as an event notification.
The publisher of an event message over a channel.
A conduit in which an event producer transmits a message. Event consumers subscribe to the channel to receive messages.
A subscriber to a channel that receives messages from the channel.
Defining Objects and Fields in Platform Events:
Platform Events can be created just like the custom objects. The biggest difference between platform events and custom object is suffix name for the API name. In platform events, its API name suffix with __e where the custom object appends __c suffix to create API name. Unlike with custom objects, we cannot update or delete event record, or view event records in Salesforce User Interface.
Platform Events support only following custom fields type,
- Text Area (Long)
ReplayID System Field and Event Retention
- Salesforce stores platform events for 24 hours. In addition you can retrieve stored events in API clients but not in Apex.
- Each event record contains a field called ReplayID, which is populated by system after the event is published.
- Each ReplayID is guaranteed to be higher than the ID of the previous event, but not necessarily contiguous for consecutive events.
- You can retrieve all stored events, or you can specify the ReplayID of an event as the baseline for the retrieved portion of events.
Steps to Create Platform Events:
1. Go to setup, search for platform events in quick find.
2. To create a new platform event, you need to click on the New Platform Event Button.
3. Start filling the Platform Event Details. The details would include points like Label, Description and Publish Behavior. The API name of Platform Event would have a postfix with __e, which shows that it’s an event object. Publish Behavior is of 2 types
a) Publish Immediately-
- It don’t respect transaction boundaries & can’t be rolled back.
- Event will be published even if the transaction is failed.
- It can be used for logging purpose which is intended for transaction.
b) Publish After Commit–
- Published only after transaction commits.
- If transaction commit then only event published. Moreover if any error occur then event is not published.
- Transactional and can be rolled back.
- Define event with this option when you are sure that transaction will commit.
4. Create custom fields for the defined platform event.
Ways to Publish/Subscribe Platform Events in Salesforce:
Ways to Publishing:
1. Process Builder
4. API ( REST/SOAP)
Steps to Publish the Platform Event through Flow Builder
1. Go to setup, search for flows in quick find. Click on New Flow.
2. Select the Screen Flow, drag and drop the required fields for creating a form to get information from end user through which you can publish the data for creating an Employee record.
3. Add a Create Record element then choose the created platform event object(Job Search) and map the custom fields with the end user input. Once the platform event record gets created, the event messages are transferred through the Event Channel.
4. Finally connect the elements, save and activate the Flow.
Ways to Subscribing:
1. Process Builder
3. Apex Trigger
Steps to Subscribe to the Platform Event through Flow Builder
1. Create a new flow and select Platform Event-Triggered Flow.
2. Choose respective platform event object(Job Search) to subscribe in the Start element.
3. Add a Create Record element then choose the created Salesforce object(Employee) and map the custom fields with with published platform event field values(Job Search).
4. Add an Action Element and choose Email from the category to send an Email notification to the respective end user.
5. Finally connect the elements, save and activate the Flow.
Collecting the input data from end user using Screen Flow
From the Screen we get the inputs from the user. While running the flow, it shows the screen to get the real-time data from the user and based on the input the platform event gets published and in this scenario a record of Employee Object is created.
Store the input and send an email using Platform Event-Triggered Flow
By using the Platform Event-Triggered Flow, we can get and store the information in Salesforce. The Action element in flows has an option to send an email to the user based on the published input. In addition, we can optionally use the event messages in the email template subject and body as merge fields.
In conclusion, the Platform Events are quite secured as well as scalable and one can easily define various fields to personalize the platform event data.
Platform Events are an important part of Salesforce’s enterprise messaging platform. The platform events which is an event-driven messaging architecture that would help in enabling the apps to communicate well, both inside as well as outside of Salesforce.