Sunday, 19 April 2020

Platform Events in Salesforce


Platform Event is based on Event-Driven Architecture which enable apps to communicate inside and outside of Salesforce. Platform events are based on the publish/subscribe model and work directly with a message bus which handles the queue of incoming events and processes listening for them. This is built in real time integration patterns in the Salesforce Platform which helps to reduce point-to-point integration

Here is some terminology we should remember :-
  • Event : A change in state that is meaningful in a business process.
  • Event message / Notification : A message that contains data about the event.
  • Event producer : The publisher of an event message over a channel.
  • Channel : A conduit in which an event producer transmits a message. Event consumers subscribe to the channel to receive messages. Also referred to as event bus in Salesforce.
  • Event consumer : A subscriber to a channel that receives messages from the channel.

Understanding of Platform Event

  • SObject like Salesforce Entity
    • Suffixed with __e
    • ReplayId fir replaying specific event
    • Only Checkbox, Date, Date/Time , Number , Text and Text Area field available.
  • Pub / Sub based communication 
    • No Polling required.
  • Heterogeneous playloads
    • Define events with different playloads

Difference between SObject and Platform Events


SObjects__c
Platform_Events__e
DMLs (Insert, Update, Delete)
Publish (Insert only)
SOQL
Streaming API
Triggers
Subscribers
Parallel context execution
Guaranteed order of execution

Considerations :-

  1. Platform event is appended with__e suffix for API name of the event.
  2. You can not query Platform events through SOQL or SOSL.
  3. You can not use Platform in reports, list views, and search. Platform events don’t have an associated tab
  4. Published platform events can’t be rolled back.
  5. All platform event fields are read-only by default
  6. Only after insert Triggers Are Supported
  7. You can access platform events both through API and declaratively
  8. You can control platform events though Profiles and permissions

Publishing / Subscribing Platform Events



Publish Platform Events :


We can publish the platform events in 3 ways:
  1. Publish Events Messaging using APEX.
    List<Order_Shipping__e> orderShippingList = new List<Order_Shipping__e>();
    Order_Shipping__e orderShipping = new Order_Shipping__e( Order_Number__c='12345', status__c=1 );
    orderShippingList.add(orderShipping);

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

    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() );
            }
        }
    }
  2. Publish Events Messaging using Declarative tools 
    1. Process Builder 
    2. Cloud Flow Designer Tool / Visual Work flow
  3. Publish Events Messaging using Salesforce API from external app.

Subscribing Platform Events :


We can subscribe the platform events with following ways :
  1. Apex Trigger : Write an “after insert” Apex trigger on the event object to subscribe to incoming events.Triggers receive event notifications from various sources—whether they’re published through Apex or APIs.

    Trigger OrderShippingTrigger on Order_Shipping__e (after Insert) {
      
    }
  2. Subscrbe to platform event notification in Lightning components
    1. Lightning web components :  Use the empApi methods in your Lightning web component, import the methods from the lightning/empApi module as follows

      import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled }
          from 'lightning/empApi';
    2. Subscibe in an Aura Component : Use the empApi methods in your Aura component, add the lightning:empApi component inside your custom component and assign an aura:id attribute to it
      <lightning:empApi aura:id="empApi"/>
  3. In an external app, you subscribe to events using CometD as well.
  4. Flow and process builder. Check this post.
Please check below two Apex Hours sessions to learn more about platform event.
  1. Platform Event basics
  2. Overcome Salesforce Governor Limits Using Platform Events
 

Further Learning



Thanks
Amit Chaudhary

3 comments:

  1. I am publishing a Platform Event for Parent and Child and its grandchild . While Cloning the Parent , its child and grandchild is also cloned , but the subscriber is not receiving it in the order of Replay Id and moreover one of its grandchild replayId is less than that of its Parent. Why does it behaves inconsistently?

    ReplyDelete
  2. Can you give a real time detailed example of Streaming API's(publish/subscribe model).

    ReplyDelete
  3. ETG is a Salesforce Commerce Cloud Implementation Partner. For 20 years, ETG Global Services, Inc. has successfully implemented eCommerce solutions for growth and enterprise clients.

    ReplyDelete