Salesforce platform events

Salesforce platform events are based on event-driven software architecture. Platform events are cloud message platform where you can publish and subscribe to an event. 

The publisher and subscriber can be a Salesforce app, external app, apex code, process builder or a flow.

You a can publish and subscribe via CometD library if you are using an external application. You can also use the CometD library inside apex code. Salesforce mainly emphasis to use platform events to integrate with external applications and vice versa. The platform events change the basic architecture of the application development in Salesforce.

Overall Architecture

Any app can publish an event which goes to event bus which will be notified to all subscribers. See the image below for pictorial representation.


Use case

The contact owner needs to be notified if a contact email is updated.

This can be achieved via multiple ways, just for the sample implementation the Salesforce platform events we use Salesforce platform events to achieve this.


You need to create platform event objects.

Custom fields to hold the email and message. 

An event publisher (Either process builder or trigger)

Event subscriber (A trigger on the event sobject).


Let's create a platform event by going to Setup and then search for platform events and click on Platform Events. Create a new platform event object. Let's say Contact_email_change__e note that the suffix of the platform event API name is __e. 

Now create two new custom fields say Message__c and OwnerEmail__c. These two fields hold the information of an event which we can utilize while subscribing to the event.

Create a process builder to publish the event whenever an email is changed in a contact.

Let's create the event subscriber, which is a trigger which sends an email to contact's owner when the event is fired.

trigger ContactEmailChangeTrigger on Contact_email_change__e (after insert) {

    list<Messaging.SingleEmailMessage> emailsToSendList= new list<Messaging.SingleEmailMessage>();

    for(Contact_email_change__e  contactEmailChangeEventForVar :{
        String[] toAddresses = new String[] { contactEmailChangeEventForVar.OwnerEmail__c}; 
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        mail.setSenderDisplayName('Salesforce Admin');
        mail.setSubject('Contact email changed');
        mail.setPlainTextBody(contactEmailChangeEventForVar.Message__c );
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();    



This is a simple implementation of Platform events. Hope this is useful.

Cheers!!! heartlaugh


No votes yet