Advanced 30 min read

Platform Events

By the end of this lesson, you'll be able to:

  • Explain what a platform event is and the publish/subscribe pattern it enables
  • Publish a platform event from Apex
  • Recognize when platform events fit better than a direct callout

Prerequisites: "SOAP Web Services Overview"

Publish/subscribe: a different integration shape

Every earlier lesson in this module was a direct, point-to-point connection: Apex calls one specific external system, or one external system calls one specific Apex endpoint. A platform event is different — Apex publishes a message, and any number of subscribers (other Apex triggers, external systems via a streaming API, automation tools) can react to it independently, without the publisher knowing or caring who's listening.

Publishing a platform event

Order_Shipped__e event = new Order_Shipped__e(
    Order_Id__c = '801XX0000000001',
    Tracking_Number__c = 'TRK-9876'
);

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

if (result.isSuccess()) {
    System.debug('Event published successfully.');
}

Order_Shipped__e — note the __e suffix, this platform's version of the __c/__mdt suffixes seen throughout this course — is a custom platform event definition, published via EventBus.publish() rather than a regular insert DML statement.

When this fits better than a direct callout

If three different systems all need to know "an order shipped" — a shipping-notification email, an external accounting system, and an internal analytics dashboard — a direct callout approach would need Apex code that explicitly calls all three, one by one, and grows more complicated every time a fourth subscriber is added. Publishing one platform event lets each subscriber react independently, with the publisher never needing to change as subscribers are added or removed — a genuinely more decoupled integration shape.

Exercise

As a comment, explain why a platform event is a better fit than three separate direct callouts for the "three systems need to know an order shipped" scenario.

Show hint

Think about what happens when a fourth subscriber needs to be added later.

APEX

Platform Events Quiz

1. What does the publish/subscribe pattern let a platform event's publisher avoid?

Log in to submit the quiz and save your score.

My Notes

Log in to keep private notes on this lesson.

Questions about this lesson

No questions yet — be the first to ask.

Log in to ask a question about this lesson.

Summary

A platform event is a message Apex can publish that any number of subscribers — inside or outside Salesforce — can react to, without the publisher needing to know who's listening.