Platform Events are just like another custom object that is used to connect External sources through Business Processes in Salesforce.
Consider a scenario where an app in salesforce notifies to an external app and in another one where an external product app notifies Salesforce for completion of manufacturing. So here salesforce and external system communicate through Platform event messages.
Platform Events are part of the salesforce enterprising messaging platform and this platform provides an Event-driven architecture that enables the app to communicate inside and outside of Salesforce.
Event-Driven Software Architecture
An event-driven (or message-driven) software architecture comprises of:
- Event Producers
- Event Consumers
In a large distributed system this architecture is suitable because it decouples event producers from consumers and therefore it simplifies the communication model in a connected system.
Some Important Components of Event-Driven Systems:
Event: Event is defined as a change in state.
Event message: Event message is also known as event Notification. It contains data about the event.
Event Producer: It is a publisher of Event Message.
Event Consumer: It is defined as who receives messages or notifications from the event bus.
Channel: It is also known as Event bus in Salesforce through which an event producer sends a Message and consumer Subscriber to the channel to receive messages or notifications.
Goals of Salesforce Platform Events:
The goal is to extend the existing capabilities of Integration options such as Outbound messages, Streaming API, Apex Callouts.
Use of Salesforce Platform Events in External Apps :
Platform Events enable the flow of event messages within Salesforce and or from External apps. Apps on salesforce can use Apex method, Apex trigger, or the empApi Lightning component to consume events. Declarative tools, such as Process Builder or Flow.
Events can be published in External apps using the SObject API and consumed by using CometD. There is a lot of flexibility to choose accordingly to use Platform Events!
Basic Flow Of Salesforce Platform Events:
- Define a platform Event In Salesforce.
- Use a Trigger to fire it.
- An External app (in case You need) to subscribe to events
- Listen to Update what triggered your event.
Procedure to create Platform Events in Salesforce:
- Go to setup and search platform Events in Quick search box.
- Click on New platform Events.
- Adding Description is totally Optional.
- Now choose Publish Behaviour – You have 2 options Now
- Publish after commit- Choose this if you want to Publish Event Message after a transaction commits Successfully.
- Publish Immediately- Select this if you want to Publish Message regardless of the transaction.
- Then click Save.
Subscribe to Events:
- By apex Triggers.
- Using visual force or Lightning component (CometD is used to receive events here)
Points To Remember:
- Platform events are not processed within the database transaction, therefore they can’t be rolled back.
- Salesforce Stores Event For 24 hrs only.
- Salesforce can also be a publisher or subscriber and doesn’t even need to know the source or destination.
- Platform Events are Non-transactional.