Understanding Platform Events in Salesforce: A Complete Guide
📌 Introduction
In today’s connected world, seamless communication between systems is vital. Whether you’re integrating Salesforce with external systems or designing real-time workflows within Salesforce itself, Platform Events offer a robust solution to build event-driven architectures.
Salesforce introduced Platform Events to enable real-time, scalable, and secure event-based communication inside the Salesforce ecosystem and beyond.
🚀 What are Platform Events?
Platform Events are a part of Salesforce’s Event-Driven Architecture. Think of them as custom, publish–subscribe events built natively into Salesforce.
They allow apps (both inside and outside of Salesforce) to communicate by publishing and subscribing to event messages.
These events behave like custom objects but are optimized for event delivery rather than data storage.
🔄 Publish-Subscribe Model
The Platform Events model works on the Publish-Subscribe principle:
– Publisher: A component or system that sends (publishes) an event.
– Subscriber: A component or system that listens (subscribes) and reacts when that event occurs.
📦 Real-Life Use Case
Imagine an eCommerce system where a customer places an order. The moment the order is placed:
– The order management system publishes a `Order_Placed__e` Platform Event.
– Salesforce, subscribed to this event, receives it and automatically creates a case, sends an email, or updates an internal record.
🛠️ How to Create a Platform Event
1. Go to Setup.
2. Search for Platform Events in the Quick Find box.
3. Click New Platform Event.
4. Provide a Label and Object Name.
5. Add Custom Fields just like a standard/custom object.
Example: `Order_Notification__e` with fields like `OrderId__c`, `Status__c`, `CustomerEmail__c`.
📤 Publishing Platform Events
You can publish events using:
1. Apex Code
Order_Notification__e eventObj = new Order_Notification__e(
OrderId__c = ‘ORD123’,
Status__c = ‘Shipped’,
CustomerEmail__c = ‘user@example.com’
);
EventBus.publish(eventObj);
2. Process Builder / Flow
Use Platform Event triggers in Flows to publish based on record changes.
3. External Systems
Use Salesforce CometD API or Pub/Sub API to publish events from external apps.
📥 Subscribing to Platform Events
1. Apex Trigger
trigger OrderNotificationTrigger on Order_Notification__e (after insert) {
for (Order_Notification__e evt : Trigger.New) {
System.debug(‘Order Received: ‘ + evt.OrderId__c);
}
}
2. Lightning Components / LWC
import { subscribe, MessageContext } from ‘lightning/empApi’;
subscribe(‘/event/Order_Notification__e’, -1, (message) => {
console.log(‘New order received: ‘, message.data.payload);
});
3. External Systems
Use CometD or Pub/Sub API to listen to Platform Events externally in real time.
⚙️ Platform Event Features
| Feature | Details |
|————————–|————————————————————————-|
| Delivery | At least once (reliable, not guaranteed exactly once) |
| Storage | Retained for 72 hours or 1 million events (whichever comes first) |
| Limits | Governed by platform limits (e.g., daily published event limits) |
| Transactions | Can be part of Apex transactions |
| Ordering | Guaranteed only per publisher |
🧠 Tips and Best Practices
– Use trigger context variables wisely to avoid recursion.
– Always check for limits (e.g., publish limits per license).
– Design subscribers to be idempotent (handle duplicate events gracefully).
– Use Custom Metadata to make event-driven logic more configurable.
✅ When to Use Platform Events?
– You need real-time communication between Salesforce and external systems.
– You want to decouple your logic using event-driven architecture.
– You’re building complex integrations where updates need to be instantly broadcast.
❌ When NOT to Use Platform Events
– You need exact delivery once and guaranteed ordering across publishers.
– You require persistent, queryable storage like with standard/custom objects.
– Your volume consistently exceeds event publishing limits.
📚 Conclusion
Platform Events in Salesforce empower developers and architects to build reactive, real-time apps that scale. With proper planning and implementation, you can achieve loosely coupled, highly efficient integrations within and outside of Salesforce.
🧩 Resources
– Salesforce Platform Events Developer Guide: https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/
– Trailhead: Platform Event Basics: https://trailhead.salesforce.com/en/content/learn/modules/platform-events-basics