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.
What Are Platform Events in Salesforce?
Platform Events are used to deliver notification within Salesforce or external app. It is based on Event-Driven Architecture. Platform event are helpful to overcome with point to point Salesforce Integration challenges.
Learn about how to Integrating with Salesforce using Platform Events.
Platform Events Terminology
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
Let understand the platform event in details.
- 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 |
Publishing / Subscribing Platform Events
Let understand how we cab publish and subscribe the platform events in Salesforce.
Publish Platform Events
You can publish event messages from a Force.com app or an external app using Apex or Salesforce APIs and you can subscribe from the Salesforce or external apps or use long polling with cometD as well. We can publish the platform events in 3 ways:
1. 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(orderShippingList);
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() );
}
}
}
2. Flow/ Process Builder
Publish Events Messaging using Declarative tools
- Process Builder
- Salesforce Flow Builder
Learn more about Platform Event Trigger flow.
3. Events Messaging using Salesforce API from external app
You can Publish Events Messaging using Salesforce API from external app. Here is sample of executing it from Salesforce workbench.
Subscribing Platform Events
We can subscribe the platform events with following ways.
1. 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) {
}
2. Subscribe 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 follows.
import { 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"/>
3. In an external app
you subscribe to events using CometD as well.
4. Flow and process builder.
Check this post.
Check our FREE Salesforce Training
10+ FREE Salesforce Training
When to use platform events in Salesforce
Overcome Salesforce Governor Limits Using Platform Events. Mixed DML’S Operations, Too Many SOQL Queries, Too Many DML Statements, CPU Timeout: Salesforce’s Governor limits are there for a reason but even when you employ best practices you may still exceed them. A good developer will look at all tools available on the platform and find the best approach to solving the problem they are facing.
Platform Events 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.
Summary
Platform events simplify the process of communicating changes and responding to events. Platform events can be used to Overcome Salesforce Governor Limits.
newsEventList is not declared.
New posts