Publishing Platform Events from Apex

Sharing is Caring

As a Salesforce developer, you may want to trigger certain actions when specific events occur within your organization’s Salesforce. Triggering Platform events from within Salesforce is pretty easy.

This post part of the blog post series: Integrating Salesforce With Other Apps – A Series! 

What is a Platform Event?

A Platform Event is a custom message that can be published and subscribed to on the Salesforce platform.

Platform Events provide a way to decouple different components of an application and allow them to communicate with each other asynchronously. For example, a Platform Event can be used to trigger an integration with an external system, or to update a dashboard in real-time when a certain event occurs.

One of the key benefits of using Platform Events is that they are scalable and can handle high volumes of events. They are designed to be reliable and can be published and consumed across multiple Salesforce orgs, as well as external systems.

Platform Event Limits

Like everything in a multi-tenant SaaS or PaaS there are some limits that are enforced so everyone has access.

Custom Platform Events have a limit of 1 MB, Salesforce’s generic events have a limit of 3 KB. There are limits on how much data can be sent per 24 hour period,

So, how do we define custom Platform Events?

Step 1: Define the Platform Event To create a Platform Event, navigate to the Salesforce setup menu and select “Integrations”. Then choose “Platform Events”.

Step 2: Then click the “New Platform Event” button to create a new event. In the new Platform Event form, you can set properties like the event name, description, and fields.

After you click “New Platform Event” you end up on a form that only has a few required fields. If you have regularly, created new objects in Salesforce you know the form really well.

I have highlighted the required fields in yellow.

After defining your Platform Event, save it.

We can then enter Edit mode, and add custom fields.

So, how do we Publish a Platform Event in Apex?

To fire a Platform Event from Apex code, you can use the EventBus.publish() method. This method takes an instance of your Platform Event as a parameter, and publishes it to all subscribers.

EventBus.publish() returns a Database.SaveResult Here’s some example code:

MyPlatformEvent__e myEvent = new MyPlatformEvent__e();
myEvent.Field1__c = 'Hello';
myEvent.Field2__c = 'World';

In this example, we first create an instance of the MyPlatformEvent__e event and set the values of its Field1__c and Field2__c fields. Then we call the EventBus.publish() method, passing in our Platform Event instance. This publishes the event to all subscribers, triggering any actions or processes that are listening for this event.

Step 3: Subscribe to the Platform Event

To perform actions in response to a Platform Event, you’ll need to subscribe to it. This can be done using Apex triggers or Process Builder. Here’s an example of how to create a trigger that listens for our MyPlatformEvent__e event and performs an action when it is fired:

trigger MyPlatformEventTrigger on MyPlatformEvent__e (after insert) {
    for (MyPlatformEvent__e event : {
        // Perform some action here

When the trigger is fired, it iterates over all the new events in the list and performs some action.

There are a lot of best practices for triggers. Most importantly, be sure to use a Trigger Handler, and Bulkification.

Things to Watch for With Platform Events

Platform Events are incredibly powerful and flexible, but there are pitfalls to watch for. Here are some things to keep in mind:

  1. Execution Limits: Publishing Platform Events from Apex code can consume CPU time, heap size, and other resources, which can impact org performance. Be sure to monitor your org’s limits and governor limits to ensure that you stay within the recommended limits.
  2. Asynchronous Processing: Platform Events are designed to be processed asynchronously, which means that they can be published and consumed in a different order. As a result, you should be careful when designing your event-driven architecture to avoid race conditions and ensure that your events are processed correctly.
  3. Error Handling: When publishing Platform Events from Apex code, it’s important to handle any errors that may occur, such as validation errors or data type mismatches. Be sure to include error handling in your code to avoid unexpected behavior or data loss.
  4. Security: Platform Events can contain sensitive data, so it’s important to ensure that you have the proper security controls in place to prevent unauthorized access. For example, you may want to use Apex Sharing Rules to control who can view or edit certain events, or encrypt the data in your events to prevent unauthorized access.
  5. Testing: When testing your code that publishes Platform Events, be sure to test both positive and negative scenarios, such as publishing events with valid and invalid data, and testing for different types of errors.

Wrapping it Up

A Platform Event is a custom message that can be published and subscribed to on the Salesforce platform.

The post outlined the steps to define a custom platform event, publish it using Apex code, and subscribe to it using Apex triggers. The post also highlighted some things to watch out for, such as execution limits, asynchronous processing, error handling, security, and testing.

Sharing is Caring

Brian is a software architect and technology leader living in Niagara Falls with 13+ years of development experience. He is passionate about automation, business process re-engineering, and building a better tomorrow.

Brian is a proud father of four: two boys, and two girls and has been happily married to Crystal for more than ten years. From time to time, Brian may post about his faith, his family, and definitely about technology.