Understanding Platform Events using Real-Time Business use case -Salesforce

Consider the below schema,

here are 2 programs right now,

  1. Covid Vaccine details: which saves data of vaccines availability
  2. NewsLetter: for General news.

and you can subscribe to either one or both, depending on your choice through Subscribe Program.

Now the requirement is to design a way, through which the subscribers get notified, whenever there is a vaccine available at their place or some latest news has come.

The Very First Solution

Each program will take care of its own notifications process. They should be having the processes to send notifications whenever new data arrives. For this, we can have triggers written or maybe some Process builder setup on each program to send notifications.

The basic issue with this approach is, as soon we introduce new programs, we need to write and manage notification logics for them too. The approach is not flexible enough to adapt to new programs with minimal changes.

The Second Solution

We can have a Batch process that runs every day and takes care of sending emails by observing subscribers and the programs for which they have subscribed. Here we have centralized the notification process which is independent of the number of programs in the system. Even if we add or delete any program, we don't need to worry about notification logic.

The basic issue with this approach is, it is not suitable for real-time programs. For example, Vaccine is available in the morning, you got the notification in the evening because the batch has been scheduled for End of the day or maybe night. It’s hard to define the correct frequency to run the batch process, in order to make it work in Real-Time.

The Third Approach — Platform Event

Platform Events is nothing but a Publish/Subscribe model, in which Publisher publishes an event, and subscribers get notified.

The Publish/Subscribe model, also known as the pub/sub model, is an architectural design pattern that provides a framework for exchanging messages between publishers and subscribers. This pattern involves the publisher and the subscriber relying on a message broker that relays messages from the publisher to the subscribers.

You can think of a kind of Observer Pattern, where you are observing a subject that notifies you whenever something happens.

So in our case, Every program will publish its events. And Platform event being a message broker takes all events and notifies the respective subscriber in real-time.

Let’s set up a platform event as shown below,

After a platform event has been defined in your Salesforce org, publish event messages from a Salesforce app using processes, flows, or Apex or an external app using Salesforce APIs.

Let’s see, Messages with Apex as of now (you can read other ways through hyperlinks) by writing a trigger on the Covid Vaccine Details program.

You can write another use case too like,

  1. Any update has happened on a record and the event has already been sent or not.
  2. Deletion of record etc.
  3. In the same way, we can have a trigger on the Newsletter program.

(It’s a bad practice to write logic in a trigger. Always write handlers )

Now, Once the publisher has completed their task. What’s next?

Now, The Platform event (Notification) comes into the picture which takes the events from the event bus and does the required actions.

But, How will a platform event get to know that there is an event?

The EventBus.publish(something) is quite similar to “insert” operation in Salesforce DML operations. So as soon as an “insert” has happened, respective object’s trigger comes into picture.

In the same way, Platform Event has triggers that act’s as a listener for Event Bus and then work as Service providers.

In our example, We have NotificationEventTrigger. In this, we have to provide the below services,

  1. Take the events from the event bus — it comes in the form of “after insert”
  2. Get all subscribers (can have an active check to minimize the processing)
  3. Notify subscribers based on their subscriptions, using Subscribe__c records.

Some Key points Regarding the above code—

  1. The context in which the Platform event trigger runs is Automated Process.
  2. Since Automated Process is not a user, we need to use OrgWideEmailAddress, as the sender’s address.

In order to debug, Platform Event’s trigger, You can Set debug logs for Automated Process.

(Again, It’s a bad practice to write logic in a trigger. Always write handlers )

Let’s Run it.

Create a Covid Vaccine Detail Record :

Checked logs,

Now, let’s check debug logs for Automated Process.

Let’s open it. (At line 43, we have put a debug log in Notification Trigger)

Check email,

The same system will work for Newsletter Program too.

Some Considerations for Publishing and Subscribing to Platform Events with Apex and APIs

  1. Support Only for after insert Triggers
  2. Be careful when publishing events from triggers because you could get into an infinite trigger loop and exceed limits. For example, if you publish an event from a trigger that’s associated with the same event object, the trigger is fired in an infinite loop.
  3. With the default Automated Process running user, sending an email message from a platform event trigger using the Messaging.SingleEmailMessage class isn’t supported. The email can’t be sent because the sender is the Automated Process entity, which has no email address. To send an email, change the running user of the trigger.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Vimal Tiwari

Vimal Tiwari

Engineer @Google | Content Creator (Linkedin, Quora & Medium) | https://vimaltiwari2612.github.io