Platform Events

It is a publisher-subscriber model—a sender broadcasts a message that one or more receivers capture. It’s like radio transmission—a transmitter tower broadcasts a radio signal, and receivers get the signal if they’re tuned to the right frequency.

Event-Driven Software Architecture

The Salesforce enterprise messaging platform provides the delivery of secure and scalable custom notifications within Salesforce and from external sources. With platform events, you can monitor your systems and communicate changes to other systems.

Components of Event-Driven Systems

Before we go any further, let’s define some terms.

Event - A change in state that is meaningful in a business process.
Event message - A message that contains data about the event. Also known as an event notification.
Event producer - The publisher of an event message over a channel. For example, an order placement app.
Event channel - A stream of events on which an event producer sends event messages and event consumers to read those messages. Also called an event bus in Salesforce.
Event consumer - A subscriber to a channel that receives messages from the channel. For example, an order fulfillment app that is notified of new orders.

When to Use Platform Events

  • Platform to External App: Order Fulfillment in a Vendor App
  • External App to Platform App: Process Merchandise Returns in Salesforce
  • Platform to Platform: Reassign Lead Records

Platform Events and sObjects

A platform event is a special kind of Salesforce entity, similar in many ways to an sObject. An event message is an instance of a platform event, similar to how a record is an instance of a custom object. Unlike with custom objects, you can’t update or delete event records.

You can define platform event similar like custom object, go to setup –> develope –> Platform events –> create new platform events as shown below.

Few Key points of Platform Events -

  • Salesforce stores platform events for 24 hours.
  • Salesforce retains event records temporarily, you can’t query them through SOQL or SOSL.
  • Platform event is appended the __e suffix for API name of the event.
  • Platform Event doesn't support reports, list views, and search.
  • Published platform events can’t be rolled back.
  • All platform event fields are read-only by default
  • Only after insert Triggers Are Supported.
  • You can set read and create permissions for platform events. You grant permissions to users in profiles or in permission sets.
  • The maximum batch size in a platform event trigger is 2,000 event messages, which is larger than the Salesforce object trigger batch size of 200.

Publishing and subscribing Platform events

Publish Events
If your app is on the Salesforce platform, you can publish events using an Apex method or with declarative tools, such as Process Builder or Flow Builder. If your app is an external app, you can publish events using Salesforce APIs.

Publish Event Messages Using Apex

// Create an instance of the event and store it in the newsEvent variable
Order_Event__e orderEvent = new Order_Event__e(
           Address__c='New York', 
           Is_Shipped__c=true, 
           Order_Desc__c='Incldes box and assesories.');
// Call method to publish events
Database.SaveResult sr = EventBus.publish(orderEvent);
// Inspect publishing result 
if (sr.isSuccess()) {
    System.debug('Successfully published event.');
} else {
    for(Database.Error err : sr.getErrors()) {
        System.debug('Error returned: ' +
                     err.getStatusCode() +
                     ' - ' +
                     err.getMessage());
    }
}

Subscribe to Platform Events
Now that you’ve seen how to publish platform events, how do you subscribe to them to be notified of the latest news or of the shipment of a package? On the Salesforce Platform, Apex triggers, processes, flows, and the empApi Lightning component receive event notifications. Visualforce apps receive events through CometD. In an external app, you subscribe to events using CometD as well.

Subscribe to Platform Event Notifications with Apex Triggers

// Trigger for listening to Cloud_News events.
trigger OrderEventTrigger on Order_Event__e (after insert) {    
    // List to hold all cases to be created.
    List<Case> cases = new List<Case>();
     
    // Iterate through each notification.
    for (Cloud_News__e event : Trigger.New) {
            // Create Case to dispatch new team.
            Case cs = new Case();
            cs.Priority = 'High';
            cs.Subject = 'Order dispatched to ' + 
                event.Location__c;
            cases.add(cs);
   }
    
    // Insert all cases corresponding to events received.
    insert cases;
}

Leave a Reply

Your email address will not be published. Required fields are marked *