DEV Community

Neha
Neha

Posted on

Salesforce Platform Events: Build Event-Driven Applications With Apex

Modern applications often need to communicate without waiting for every process to finish immediately.

For example, when a customer places an order, Salesforce may need to notify an inventory system, update another application, or trigger additional processing.

Running every operation inside the same transaction can make the system harder to maintain.

This is where Salesforce Platform Events become useful. They allow applications to communicate through an event-driven architecture, helping separate processes and reduce direct dependencies between systems.

  1. What Are Salesforce Platform Events?

Platform Events are Salesforce event messages used to communicate information between applications and processes.

Instead of directly calling another component, a publisher sends an event. Subscribers can then receive and process that event.

A simple example:

Order Created → Platform Event Published → Subscriber Processes Order

This approach helps different parts of an application communicate without requiring them to execute together.

  1. Create a Custom Platform Event

First, create a Platform Event in Salesforce Setup.

Navigate to:

Setup → Platform Events → New Platform Event

For an order notification example, create an event named Order_Notification__e.

Add custom fields such as:

Order Number

Customer ID

Order Status

Total Amount

These fields contain the information subscribers need to process the event.

  1. Publish an Event Using Apex

Once the Platform Event is created, you can publish it using Apex.

public class OrderEventPublisher {

public static void publishOrderEvent() {

    Order_Notification__e eventMessage =
        new Order_Notification__e(
            Order_Number__c = 'ORD-1001',
            Order_Status__c = 'Confirmed',
            Total_Amount__c = 1500
        );

    Database.SaveResult result =
        EventBus.publish(eventMessage);

    if (result.isSuccess()) {
        System.debug('Event published successfully');
    } else {
        System.debug('Event publishing failed');
    }
}
Enter fullscreen mode Exit fullscreen mode

}

This example creates and publishes an event containing order information.

The publish call's success indicates that Salesforce accepted the event for publication. It does not guarantee that every subscriber has successfully processed it.

  1. Subscribe to Platform Events Using Apex

After publishing an event, a subscriber can receive and process it.

Create an Apex trigger on the Platform Event:

trigger OrderEventTrigger
on Order_Notification__e (after insert) {

for (Order_Notification__e eventMessage :
     Trigger.New) {

    System.debug(
        'Order Number: ' +
        eventMessage.Order_Number__c
    );

    System.debug(
        'Order Status: ' +
        eventMessage.Order_Status__c
    );
}
Enter fullscreen mode Exit fullscreen mode

}

This trigger runs when the event is delivered to the subscriber.

You can replace the debug statements with suitable business logic, such as updating records or initiating additional processing.

  1. Understand Publish Behavior

Platform Events support different publishing behaviors.

Two important options are:

Publish Immediately: The event is published without waiting for the current transaction to commit.

Publish After Commit: The event is published after the current transaction successfully commits.

For events that announce a database change, Publish After Commit is often useful because subscribers should not process information about a record change that eventually rolls back.

Choose the publishing behavior according to the business requirement.

  1. Test Platform Events in Apex

Testing is important because event publishing and event processing happen through separate steps.

Salesforce provides Test.getEventBus().deliver() to deliver queued test events to subscribers during testing.

A simplified test example:

@IsTest
private class OrderEventPublisherTest {

@IsTest
static void testEventPublishing() {

    Test.startTest();

    OrderEventPublisher.publishOrderEvent();

    Test.getEventBus().deliver();

    Test.stopTest();

    System.assert(true, 'Test completed');
}
Enter fullscreen mode Exit fullscreen mode

}

For production-quality tests, add assertions that verify the expected subscriber behavior. A simple successful execution does not prove that the correct records were processed.

  1. Follow These Development Best Practices

When building event-driven Salesforce applications:

Keep event payloads focused on necessary information.

Choose the correct publishing behavior.

Design subscribers to handle failures.

Avoid assuming that events are delivered exactly once.

Use replay IDs and event replay capabilities where appropriate.

Monitor event usage and subscriber performance.

Test both successful and unsuccessful processing scenarios.

Also remember that Platform Events have allocation limits and retention rules. Review Salesforce documentation before designing high-volume event solutions.

Final Thoughts

Salesforce Platform Events provide a useful way to build event-driven applications and reduce direct dependencies between systems.

By separating publishers and subscribers, developers can create more flexible integrations and asynchronous business processes.

Start with a clear event structure, choose the appropriate publishing behavior, and make subscriber logic reliable and testable.

For more Salesforce development and integration guidance, explore iBirds Software Services.

Discussion: Have you used Platform Events for Salesforce integrations, or do you prefer Queueable Apex for asynchronous processing?

Salesforce #Apex #PlatformEvents #Integration #Development

Top comments (0)