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 published, he needs to define platform event fields.

Let’s check out this post to learn 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.

What Are Platform Events in Salesforce?

Platform Events in Salesforce are a powerful way to facilitate real-time data exchange both within the Salesforce platform and between Salesforce and external systems. They operate on a publish-subscribe model, which efficiently handles events and notifications in complex systems.

How Platform Events Work:

1. Event Producer: This is the originator of the event. It creates an event that needs to be communicated to other parts of the system or to external systems. For example, this could be a change in a record or a custom event triggered by a user action.

2. Event Bus (Channel): Once an event is produced, it’s placed on the event bus, which acts like a channel. This bus is essentially a queue that maintains events in a strict chronological order. The events are then processed one after the other.

3. Event Consumers: These are the subscribers to the event. They ‘listen’ for events on the event bus. As soon as their subscribed event is placed on the bus, they are notified. An event consumer could be a process, an Apex class, or an external application using technologies like CometD, which underpins the Salesforce Streaming API.

Key Advantages of Platform Events

  • Decoupling of Systems: The event-driven architecture ensures that the event consumers do not need to have direct knowledge of the event producers. This separation reduces dependencies and enhances system resilience.
  • Flexibility: Different types of consumers can subscribe to the same event, making it a versatile tool for coordinating complex workflows.
Architecture With Platform Events’ by Florian Hoehn
Source: Salesforceben

Examples of Event Consumers in Salesforce:

  • Processes: Automated workflows or flows that can trigger actions based on the event.
  • Apex: Custom Apex classes can subscribe to events and execute logic when an event occurs.
  • CometD and Streaming API: External applications can use CometD, a scalable HTTP-based event routing bus, to subscribe to Salesforce events. This is particularly useful for real-time data synchronization between Salesforce and external systems.
how event producers function in a Salesforce platform
Source: Salesforceben
Architecture With Platform Events’ by Florian Hoehn
Source: Salesforceben

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 custom PEs publish and process millions of events effectively and 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 the 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 in defining, publishing, and subscribing to platform events. To know more, don’t hesitate to connect with us. Emizentech, a Salesforce development company, will always serve you the best.

Avatar photo
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.

whatsapp