Publish/Subscribe a Platform Event Using Apex Code and Apex Triggers

Publish a Platform Event Using Apex Code

To publish event messages, you create an instance of the event and pass it to the EventBus.publish() method.

Subscribe a Platform Event Using Apex Trigger

You simply write an after insert Apex trigger on the event object to subscribe to incoming events. Triggers provide an autosubscription mechanism in Apex.

Platform events support only after insert triggers. After an event message is published, the after insert trigger is fired.

Problem Statement

When an Account is updated with Customer Priority Field as high, update the priority field related case records with same value. However, the last modified by of those updated case record should not be the one who modified the account record.

Important Notes

Order of Event Processing

A trigger processes platform event notifications sequentially in the order they’re received. The order of events is based on the event replay ID.

Asynchronous Trigger Execution

A platform event trigger runs in its own process asynchronously and isn’t part of the transaction that published the event.

There might be a delay between when an event is published and when the trigger processes the event.

Automated Process System User

Platform event triggers don’t run under the user who executes them (the running user) but under the Automated Process system user

Please refer below video for detailed explainaion with example :


Subscribe to Platform Event Using Process Builder

We can Subscribe a platform event using:

Process Builder


Apex Triggers

Lightning Web Component

Aura Component

External App Using CometD

Problem Statement

Whenever an Deadline field on  account is updated, Deadline field on all related cases should be updated with same value.

Please refer below video for detailed explaination:

Define Platform Events in Salesforce

A platform event is a special kind of Salesforce entity, similar in many ways to an sObject. 

An event message is an instance of a platform event, similar to how a record is an instance of a custom object.

You create a platform event definition by giving it a name and adding custom fields.

We cannot:

Update an event record

Delete an event record

View an event record

We can set read and create permissions for platform events using either profiles and permission sets.

We can’t query them through SOQL or SOSL. Similarly, we can’t use event records in the user interface in reports, list views, and search.

Define a platform Event in Salesforce Org.

Considerations of Platform Events:

Publish After Commit

Transactional and can be rolled back.

Published only after a transaction commits successfully.

Use this  if subscribers rely on data that the publishing transaction commits.

Use this when you don’t want the event message to be published if the transaction fails.

Publish Immediately

Not transactional and cannot be rolled back.

If you want the event message to be published regardless of whether the transaction succeeds.

Subscribers don’t rely on data committed by the publisher.

Standard-volume events

Events that were defined before Spring ’19 are stored for 24 hours in the event bus.

You can no longer define such events.

High-volume events

Newly defined events are high volume by default.

Salesforce stores high-volume platform events for 72 hours in the event bus

Please check below video for more detailed explaination:

Event-Driven Software Architecture in Salesforce

The paradigm of event-based communication revolves around a publisher-subscriber model where a sender broadcasts a message that one or more receivers capture.

Events get sent whether or not receivers are listening, and receivers don’t acknowledge it when they receive an event.

Event-based communication takes place near-real time.

Components of Event-Driven Systems


A change in state that is meaningful in a business process. 

Event message

A message that contains data about the event.

Event producer

The publisher of an event message

Event channel

A stream of events on which an event producer sends event messages and event consumers read those messages.

Event consumer

A subscriber to a channel that receives messages from the channel.

Event bus

A communication and storage service that enables event streaming using the publish-subscribe model. The event bus enables the retrieval of stored event messages at any time during the retention window.

Advantages of Event Driven Architecture

Unlike request-response communication models, event-driven model decouples event producers from event consumers, simplifying the communication model in connected systems.

We don’t have to make any request  to a server to obtain information about a certain state.

Instead, a system subscribes to an event channel and is notified whenever new states occur.

Any number of consumers can receive and react to the same events. 

When to Use Platform Events:

Platform to External App

External App to Platform App

Platform to Platform

Please check below video for detailed explaination: