Salesforce Platform Events a plus to Integration Technique


Platform Events are new Salesforce publish-subscribe modal available from Summer '17 release. Salesforce platform is already powerful having Outbound Messaging, REST API, and Streaming API available and with this new Platform Events it makes it even better and reduces need of having point-to-point integrations.

Resource Links:
1. Platform Events Developer Guide Link
2. Here is an awesome blog post Link from Christopher Marzilli with detailed explanation about Platform Events

Lets see how platform events work with a small implementation:
Step 1: In Lightning experience Platform Events can be accessed using Setup-->Data-->Platform Events and here we are going to create a custom Text_Message platform event which is very similar on how we create custom objects, the only difference that we see in the API name is that all platform events end with '__e'. Also I have two custom fields (Message_Id__c, and Message_Type__c) to process message related call.



Publishing a Platform Event:
Now that we have created our Platform Event we are ready to Publish and Subscribe. For publishing an event we use EventBus.publish static method provided by EventBus class which can accept list of events or a single event, below we have an after update trigger on Account object that will check if Account Rating is 'Hot' and it will create a new event and publish it.
1:  trigger Account on Account (after update) {  
2:    //list to store platform events  
3:    List<Text_Message__e> lstTxtMsgs = new List<Text_Message__e>();  
4:    for (Account iterator: Trigger.New) {  
5:      //if account rating is hot then create a platform event  
6:      if (iterator.Rating == 'Hot') {  
7:        lstTxtMsgs.add(new Text_Message__e(Message_Id__c = iterator.Id, Message_Type__c = iterator.Rating));  
8:      }  
9:    }  
10:    //publish platfrom event  
11:    List<Database.SaveResult> rslts = EventBus.publish(lstTxtMsgs);  
12:    //loop thruogh list to debug results  
13:    for (Database.SaveResult iter: rslts) {  
14:      if (iter.isSuccess()) {  
15:        System.debug('Text Message Event was Successfully published!');  
16:      } else {  
17:        for (Database.Error err: iter.getErrors()) {  
18:          System.debug('Exception Occured: ' + err.getMessage());  
19:        }  
20:      }  
21:    }  
22:  }  
Here EventBus.publish return type is a List of Database.SaveResult, which we are using to loop through for debugging results.

Subscribing to Platform Event:
Below is our Trigger on Text_Message__e Platform Event to Subscribe our event and in our we are going to check if the created event with Message_Type as 'Hot' to create a new Case and assign it to the Account that has publish the event.
1:  trigger TextMessageTrigger on Text_Message__e (after insert) {  
2:    //list to update all case records  
3:    List<Case> lstCase = new List<Case>();  
4:    for (Text_Message__e iterator: Trigger.New) {  
5:      //create new case if message_type is hot  
6:      if (iterator.Message_Type__c == 'Hot') {  
7:        Case c = new Case();  
8:        c.AccountId = iterator.Message_Id__c;  
9:        c.Priority = 'High';  
10:        c.Subject = 'New Case for Account with Priority Hot. Account Id: ' + iterator.Message_Id__c;  
11:        lstCase.add(c);  
12:      }  
13:    }  
14:    //insert case list  
15:    insert lstCase;  
16:  }  


Once we subscribe our event, we should be able to see it under "Subscriptions" related list of our Text_Message__e platform event.


We can also get list of all Subscribers by writing a Soql query on EventSubscriber Object. Execute below Query using "Query Editor" in Developer Console.
SOQL QUERY: SELECT ExternalId, Name, Position, Status, Tip, Type FROM EventBusSubscriber Where Topic='Text_Message__e'



Now Lets look into how publishing works from API perspective by creating Text_Message__e record using Workbench.
REST Endpoint: /services/data/v39.0/sobjects/Text_Message__e/
JSON Request Body:
{ "Message_Id__c": "001E0000002MoN8IAK", "Message_Type__c": "Hot" }
Response:
HTTP/1.1 201 Created
{ "id" : "e00xx0000000001AAA", "success" : true, "errors" : [ ] }


There is another way of subscribing to platform events using CometD. Developer guide has detailed explanation on that.

With Platform Events being introduced recently we also need to consider on some Limitations that Salesforce has on them. Below are some Limitations with Platform Events.
1. SOQL queries are not supported on Platform Events.
2. Platform Event records can only be inserted and they cannot be updated or deleted. This also means that we can only have after insert trigger on Platform Events.
3. Since Platform Events are inserted using event sObjects so API request limits do apply.

Finally there is something interesting about Platform Events called ReplayId which is used to get stored events that are within the retention window. Only a subscribed CometD client can retrieve past events using the ReplayId field. SOQL queries aren’t supported. This field is among the standard fields that the system populates for each platform event. Each replay ID is guaranteed to be higher than the ID of the previous event, but not necessarily contiguous for consecutive events. The ID is unique for the org and the channel.

Comments

Popular posts from this blog

Displaying Toast Message from Modal in Lightning Components

Lightning Component to display dynamic sObject data

Lightning RecordForm - An enhanced Lightning Data Service