Last Updated on January 21, 2022

In Salesforce, you can connect the business process with the external apps using Platform Events by exchanging real-time data. Platform events are scalable and secure messages holding data. The publishers publish the event messages that the subscribers receive in real-time. If one wants to modify the data publishes, he needs to define platform event fields.

Let’s check out this post to know better about the platform events.

Streaming API utilizes push technology to publish events and provides a mechanism to subscribe to these events to receive the data in near real-time. Platform Event (PE) is one such streaming API in Salesforce. It follows the fire and forget mechanism of Integration. The publisher can publish the customized payload and multiple subscribers can receive the same PE simultaneously by subscribing to that PE. It is a secure and scalable solution.

Types of Platform Events

1. Standard PE

These are the predefined events provided by Salesforce itself. Some examples of Standard PE are:

  • AssetTokenEvent: Monitors OAuth 2.0 authentication activity.
  • BatchApexErrorEvent: Reports error encountered in batch apex jobs.

2. High-Volume PE

These are custom PE used to publish and process millions of events effectively and to scale your event-based apps.

Defining Platform Events

Below are the steps to create a custom Platform Event in Salesforce:

  1. 1. In Setup, select Platform Events in the quick find.
  2. 2. Click New Platform Event.
  3. 3. Fill in the standard fields.
  4. 4. Platform Events provide two types of publishing mechanisms.
    Publish after commit is used to publish the event only after the transaction is successfully committed.
    Publish Immediately will publish the event as soon as the call is executed in run time.
  5. 5. Click Save to create the platform event.
  6. 6. We can also add custom fields to the platform event.
salesforce platform events

Publishing Platform Events

Below are several ways to publish a platform event after defining the same:

  • Publish Event Messages with Flows
  • Publish Event Messages with Processes
  • Publish Event Messages with Apex
  • Publish Event Messages via APIs

Below is the code snippet to publish platform event with Apex:

private void publishEvents(){
        //Creating PE instances
        List<Test_Event__e> platformEventList = new List<Test_Event__e>();
        Test_Event__e testEvent = new Test_Event__e();
        testEvent.Test_Field__c = 'Message included in the platform event';
        platformEventList.add(testEvent);
 
        // Call method to publish events
        Database.SaveResult[] srList = EventBus.publish(platformEventList);
        
        // Inspect publishing result
        for(Database.SaveResult srVar:srList){
            if (srVar.isSuccess()) {
                System.debug('Successfully published event.');
            } else {
                for(Database.Error err : srVar.getErrors()) {
                    System.debug('Error returned: ' +
                                 err.getStatusCode() +
                                 ' - ' +
                                 err.getMessage());
                }
            }    
        }
    }

Subscribing Platform Events

Below are the several ways to receive the platform event:

  • Subscribe to Platform Event Messages with Flows
  • Subscribe to Platform Event Messages with Processes
  • Subscribe to Platform Event Notifications with Apex Triggers
  • Subscribe to Platform Event Notifications in a Lightning Component
  • Subscribe to Platform Event Notifications with CometD(Workbench)

Subscribing to Platform Event via Workbench

  • Login to wokbench.developerforce.com
  • Navigate to Streaming Push Topics under Queries
  • Select Generic Subscriptions
  • Fill the Subscription field in the format /event/{EVENT_NAME}
  • In the above example, it should be /event/Test_Event__e
  • Click the subscribe button
streaming push topics Salesforce

Below is the JSON structure of the message received via Platform Events:

streaming push topics

Conclusion

We hope you find this post helpful and will guide you to define, publish, and subscribe to platform events. To know more, don’t hesitate to connect with us. Emizentech, a Salesforce development company, is always there to serve you the best.

Author

With a decade of experience in eCommerce technologies and CRM solutions, Virendra has been assisting businesses across the globe to harness the capabilities of information technology by developing, maintaining, and improving clients’ IT infrastructure and applications. A leader in his own rights his teammates see him as an avid researcher and a tech evangelist. To know how the team Virendra can assist your business to adopt modern technologies to simplify business processes and enhance productivity. Let’s Talk.