Pages

Contact Me

Name

Email *

Message *

Wednesday, 5 August 2020

Platform Events in Salesforce (2/2)


Now, that we have seen basics of Platform Events and Event-Driven Architecture, let's dive in deep to understand Platform Event object and supported field types.

Create a Platform Event :

Step 1: Go to Setup and find Platform Events.



Step 2: Click on New Platform Event Button




 Step 3: Fill in all the details and click Save. Let's understand some features of the Platform Event object and fields.

Platform Event Object :
  • Plateform Event object is created just like custom objects whose API Name will end with __e.
  • Unlike custom objects, we can not view/update/delete event record from UI.
  •  Platform Event records can not be queried using SOQL.
  •  Platform Event records can not used in reports and dashboards.
  • User can not roll back published event.
  • All the fields of Platform Event record would be read only by default. 

Platform Event Fields :

  • Though the Platform Event object is mostly like any other custom object, there are some limitations on supported field types. It can only support these field types: 
 
    • CheckBox
    •  Date 
    • DateTime
    • Number        
    • Text           
    • TextArea               

 
  •  Publish Behavior : It is a standard picklist field which gives users option to publish event immediately or After commit.
    • Publish Immediately : Platform Event will be pushed to Event bus immediately after triggerring an event (Button Click/ Status Change) etc.
    • Publish After Commit : Platform Event will get fired after completing all the events from Order of Execution. Use this when you want to send Id of the record as part of  event message.
 
  • ReplayId : Salesforce created a field called ReplayId after you create/publish Platform Event Record.
    • This is an system generated Id which will always be higher that previous event but not necessarly consecutive. 
    • It refers to the position of event in the Event Bus.
    • Salesforce stores ReplyId in EventBus for 24 hours.
    • Subscribers can store ReplayId and use it on resubscription to events those are in retention window.       



Publish Platform Event :

We have seen how can we publish platform events in the last Blog. We will use the apex class to publish our event. We will pass CaseId from After Insert trigger on Case.






Subscribe to the Event :

We use process builder/Flow/Apex trigger to subscribe to the Event. We can write triggers in After Insert context only if we are choosing a trigger to subscribe to an Event.





References: 


                                 



 






Platforms Events in Salesforce (Part 1/2)


Platform Events simplify the process of communication between two or more systems without writing complex logic. It is built in real-time integration patterns which reduces point to point integrations. 

Platform Events are based on an event-driven architecture which works on Publish-Subscribe pattern. Platform Event trigger runs asynchronously in a separate transaction with fresh governor limits. 

The following diagram illustrates the Publish-Subscribe (Publisher-Consumer) pattern in the Platform Event.



This Architecture consists of 3 components.

  • Event producer: Application/System which publishes messages to the Event bus/Channel.
  • Event Bus: A channel which holds all the messages from publishers. consumers subscribe to the channel to receive messages. Also referred to as the event bus in Salesforce. Order of published Events will be as per first come first serve basis.
  • Event consumer: A subscriber to a channel that receives messages from the channel.

How can a user publish the event? 
 
  • Using Apex Trigger
  • Using Process Builder/ Lightning Flow
  • Using salesforce API from external application
 
How can a user subscribe to the channel?

  • Apex Trigger
  • Process builder/ Lightning Flow
  • CometD ( The empApi Lightning component and Visualforce apps receive event notifications through CometD )