Salesforce Platform Event

Platform Events provide a way to deliver secure, scalable, and customizable event notifications within and outside of the Salesforce platform. Platform Events are based on the publish-subscribe model, allowing publishers to broadcast events and subscribers to receive and process those events in real time.

Salesforce Event Bus














Example: OrderPlaced Platform Event

Let's assume you want to create a Platform Event called OrderPlaced__e to represent the event of a new order being placed in your Salesforce org. This event will contain information about the order, such as the order ID, customer name, and total amount.

Define the Platform Event Schema:

Go to Setup > Platform Events > New Platform Event.

Define fields for the event, such as OrderID__c, CustomerName__c, and TotalAmount__c.

Publish the Platform Event:

You can publish a Platform Event using Apex, Process Builder, Flow, or other methods. Below is an example of publishing an event using Apex.

// Publish OrderPlaced event
OrderPlaced__e orderEvent = new OrderPlaced__e(
    OrderID__c = 'ORD123',
    CustomerName__c = 'John Doe',
    TotalAmount__c = 1000
);
Database.SaveResult result = EventBus.publish(orderEvent);
if (result.isSuccess()) {
    System.debug('OrderPlaced event published successfully.');
} else {
    for(Database.Error error : result.getErrors()) {
        System.debug('Error publishing OrderPlaced event: ' + error.getMessage());
    }

}

Subscribe to the Platform Event:

You can subscribe to Platform Events using Apex triggers or external systems connected to Salesforce via the Streaming API.
Below is an example of subscribing to the OrderPlaced__e event using an Apex trigger.

trigger OrderPlacedTrigger on OrderPlaced__e (after insert) {
    List<OrderPlaced__e> newEvents = Trigger.new;
    for (OrderPlaced__e event : newEvents) {
        // Process the OrderPlaced event
        System.debug('New order placed: ' + event.OrderID__c + ' by ' + event.CustomerName__c + ' for ' + event.TotalAmount__c);
    }
}

Process the Event:

Once the event is published, the trigger subscribes to the event and processes it.
In this example, the trigger logs a debug message with information about the new order.
That's it! You've created a Platform Event called OrderPlaced__e, published an event when a new order is placed, and subscribed to the event to process it in real time.

Platform events publishing demeanors

In Salesforce, there are two types of publishing behavior for platform events.

Publish after commit — Enabling this option allows for event publishing after a successful transaction commit. This feature is useful in situations where a subscriber needs to be notified upon the completion of a transaction. For instance, upon completing a checkout process, an email can be triggered to user based on the Order status.

Publish immediately — This option involves publishing the event irrespective of whether the transaction is successful. It is suitable if the subscriber does not require the process to be completed successfully. An example of this would be alerting the user about a critical error.

The publish behavior option applies to event messages published through the Lightning Platform, such as Apex, Process Builder, and Flow Builder, and through Salesforce APIs.

Platform Events provide a powerful mechanism for building event-driven architectures and integrating Salesforce with other systems in a scalable, reliable, and customizable manner. They enable organizations to create responsive, real-time applications and automate business processes based on event-driven triggers and notifications.

References

Event-Driven App Architecture On The Customer 360 Platform

Platform Events Developer Guide

Platform Events Basics

Define a Platform Event

Standard Platform Event Object List

Platform Event Considerations

Comments

Popular posts from this blog

Introduction to Salesforce Agent Script: Build Predictable AI Agents

Anti-Patterns in Salesforce Agentforce: What to Avoid When Building AI Agents

MCP vs. Traditional APIs: Choosing the Right Integration Approach