Platform Events In Salesforce

Authors: Pankuri Bansal & Nikhil Kumar

What are Platform Events?

By exchanging real-time event data, Platform Events can be used to link Salesforce business processes with third-party apps. These events are secure and scalable messages that contain data. 

How Platform Events come into the picture?
  • As Subscribe/Replay channel listeners give a single trigger for a particular object, but if you want multiple triggers (like multiple fields from multiple objects) for that we have made Platform Events.
  • The Publisher-Subscriber architecture is widely employed in event-driven systems. Here, the publishers create events that are consumed by the subscribers. This architecture has become hugely popular over time owing to its effectiveness.
  • Platform Events from Salesforce help bridge the communication gap between different applications or can be used to initiate actions in response to a certain event. They provide an easy and efficient way to share data across multiple applications.

This diagram helps to illustrate the Publisher-Subscriber architecture model, with Platform Events being represented.

  • This diagram illustrates the Publisher’s generation of the Platform Event and its publication to the Channel. As for Subscriber, it listens on this particular channel and consumes event data.
  • A clear scenario example would be integrating a Salesforce application with an external system. This is a great way to make use of AI writing assistants in the workplace. It will save time, and effort, and ultimately produce quality output that may match that of human copywriters.
  • Platform Events are highly useful for Salesforce applications that need to publish data to an external system. All you have to do is set up the Platform Event channel and your external system can subscribe and consume the data as it’s published.
  • For certain criteria, a Platform Event is the perfect way to trigger an action within Salesforce. This can be a great alternative for those who need the functionality.
  • Example: When an event like a new opportunity is created in Salesforce, you can use Platform Events to make sure it’s heard. Subscribers will then listen for this event and take appropriate actions such as notifying users via email or other updates to records in Salesforce.

The steps for creating a Platform Event are as follows:

1. Click the New Platform Event Button under Setup and then go to Platform Events.

2. Click on the save button as per the screenshot below.

3. Platform Event is created as per the below screenshot.

4. Create custom fields for the Platform Event that has been defined as per the use case requirement.

5. Depending on the specific fields you have, choose the data type for that particular custom field as shown below.

6. Give the Platform Event’s specific field name shown below.

7. Finally, all custom fields were created.

Publish Platform Events To Salesforce
Method 1: Using Record Trigger Flow:
  • Click on the New Flow under Setup and then click on Process Builder.
  • Click on New for creating the flow as shown below.
  • Click on Create in flow builder.
  • Click on Record-Trigger Flow or click the other option as per your use case requirements.
  • Choose the newly created and updated record (Order) and object.
  • Set/Configure your entry conditions as shown below.
  • The moment you click “done,” a flow is automatically created.
  • Choose “Create record” from the “Run Immediately” section as shown
  • Choose the Label name, API name, and Order as indicated.
  • Set Field Values for Order is essentially a mapping for specific object fields.
  • A flow is produced. After saving, activate it.
Method 2: Using Apex
  • This example creates two events of type demo__e, publishes them, and then checks whether the publishing was successful or errors were encountered.
  • Before you can run this snippet, define a platform event with the name of demo__e and the following fields: model__c of type Text, number__c of type Text (marked as required), Ink_Percentage__c of type Number(16, 2).
List<demo__e> lstDemoEvents= new List<demo__e>();

lstDemoEvents.add(new demo__e(model__c='XZO-5', number__c='12345',

              Ink_Percentage__c=0.2));

lstDemoEvents.add(new demo__e(model__c='MN-123', number__c='10013',

              Ink_Percentage__c=0.15));

// Call method to publish events

List<Database.SaveResult> results = EventBus.publish(lstDemoEvents);

// Inspect publishing result for each event

for (Database.SaveResult sr : results) {

    if (sr.isSuccess()) {

        System.debug('Successfully published event.');

    } else {

        for(Database.Error err : sr.getErrors()) {

            System.debug('Error returned: ' +

                        err.getStatusCode() +

                        ' - ' +

                        err.getMessage());

        }

    }       

}

References:-

https://www.salesforceben.com/salesforce-platform-events/

We use cookies on this site to enhance your user experience. For a complete overview of how we use cookies, please see our privacy policy.