Platform Event is based on Event-Driven Architecture which enable apps to communicate inside and outside of Salesforce. Platform events are based on the publish/subscribe model and work directly with a message bus which handles the queue of incoming events and processes listening for them. This is built in real time integration patterns in the Salesforce Platform which helps to reduce point-to-point integration
Here is some terminology we should remember :-
- Event : A change in state that is meaningful in a business process.
- Event message / Notification : A message that contains data about the event.
- Event producer : The publisher of an event message over a channel.
- Channel : A conduit in which an event producer transmits a message. Event consumers subscribe to the channel to receive messages. Also referred to as event bus in Salesforce.
- Event consumer : A subscriber to a channel that receives messages from the channel.
Understanding of Platform Event
- SObject like Salesforce Entity
- Suffixed with __e
- ReplayId fir replaying specific event
- Only Checkbox, Date, Date/Time , Number , Text and Text Area field available.
- Pub / Sub based communication
- No Polling required.
- Heterogeneous playloads
- Define events with different playloads
Difference between SObject and Platform Events
SObjects__c
|
Platform_Events__e
|
DMLs
(Insert, Update, Delete)
|
Publish
(Insert only)
|
SOQL
|
Streaming
API
|
Triggers
|
Subscribers
|
Parallel
context execution
|
Guaranteed
order of execution
|
Considerations :-
- Platform event is appended with__e suffix for API name of the event.
- You can not query Platform events through SOQL or SOSL.
- You can not use Platform in reports, list views, and search. Platform events don’t have an associated tab
- Published platform events can’t be rolled back.
- All platform event fields are read-only by default
- Only after insert Triggers Are Supported
- You can access platform events both through API and declaratively
- You can control platform events though Profiles and permissions
Publishing / Subscribing Platform Events
Publish Platform Events :
We can publish the platform events in 3 ways:
- Publish Events Messaging using APEX.List<Order_Shipping__e> orderShippingList = new List<Order_Shipping__e>();
Order_Shipping__e orderShipping = new Order_Shipping__e( Order_Number__c='12345', status__c=1 );
orderShippingList.add(orderShipping);
List<Database.SaveResult> results = EventBus.publish(newsEventList);
for (Database.SaveResult sr : results) {
if (sr.isSuccess()) {
System.debug('Successfully published event.');
} else {
for(Database.Error err : sr.getErrors()) {
System.debug('Error returned: ' + err.getStatusCode() );
}
}
} - Publish Events Messaging using Declarative tools
- Process Builder
- Cloud Flow Designer Tool / Visual Work flow
- Publish Events Messaging using Salesforce API from external app.
Subscribing Platform Events :
We can subscribe the platform events with following ways :
- Apex Trigger : Write an “after insert” Apex trigger on the event object to subscribe to incoming events.Triggers receive event notifications from various sources—whether they’re published through Apex or APIs.Trigger OrderShippingTrigger on Order_Shipping__e (after Insert) {
} - Subscrbe to platform event notification in Lightning components
- Lightning web components : Use the empApi methods in your Lightning web component, import the methods from the lightning/empApi module as followsimport { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled }
from 'lightning/empApi'; - Subscibe in an Aura Component : Use the empApi methods in your Aura component, add the lightning:empApi component inside your custom component and assign an aura:id attribute to it<lightning:empApi aura:id="empApi"/>
- In an external app, you subscribe to events using CometD as well.
- Flow and process builder. Check this post.
Further Learning
Thanks
Amit Chaudhary