Platform Events within the Salesforce

Why we need to use platform events ?? 

        Before Platform events were into the picture, let us say Salesforce application is integrated with SAP. Changes made to SAP table records need to be stored in Salesforce. To achieve this requirement, we were using REST / SOAP API call. As a result, API limits were consumed against our org's daily API limit ( API limit varies based on Editions, for more info on API Limits check HERE).
        Now, Salesforce has come up with a solution called Platform events. If we use Platform events, these limits will be consumed against its own limits. Check HERE, for platform events limits.

What can we do using platform events??
1. Information regarding the changes made to Salesforce SObject records are sent to the external system. This process, in other terms is also called as Callouts.
2. Changes done in the external system can be shared with Salesforce. These are called as Call-ins.
3. Within the Salesforce platform, based on changes done in one sObject, we can perform any DML operation on another sObject.
     
              In this post, we will learn, how platform events will detect changes within the salesforce using trigger & Process builder.

Requirement:
       In this example, My requirement is to update Contact's email when Account record is updated.
If Account record name is updated as "Test" then associated Contact email field value should be updated as "mm@salesforce.com".
This requirement can be achieved using 3 ways. They are as follows:
-->Triggers
-->Process Builders
-->Flows

Solution 1
   Using Triggers   
     We need to write two triggers to achieve this requirement.
  •  Trigger On Account Object:    A Trigger on Account object to publish the event. This trigger informs the apex engine that there are certain changes occurred on Account object.
  • Trigger on Platform Event Object:       A Trigger on Platform Event object to perform our desired action. As per our requirement,  this trigger will update Contact's email field (which is related to Account). 
Solution 2
     Using Process Builder
            Similar to triggers, we need to write two Process builders to achieve this requirement.
  •  Process builder On Account Object       We need to choose "Create a Record" option in process builder, "Record Type" should be Platform Event Object and the initiating object must be sobject (In our case it is Account  object). And from this process builder, we need to send AccountId to another process builder as input.

  • Process Builder on Platform Event Object:     We will see this in step by step.
        Step 1While the creation of Process Builder, we need to choose "A platform event occurs"     option.
    Step 2:  While checking the condition, check if AccountId is matching with the AccountId passed from the Process Builder which publishes Platform events.

   Step 3:  If above step is true then update the contact emailId to sample EmailId.
                                          That's it...!!! see how simple it is..;)
How to Test this ??  😟😕
   1. Check how many account records are available using below Query
2. Execute the below code snippet using execute anonymous block
3. Now all the contacts emails are updated with the sample emailId.

How to know whether my platform event Trigger / Process Builder is going smoothly or not?
 > First, check the Status of Subscriptions of the Platform event
              If it is Running then there is no issue with the Trigger/Process builder. (if the status is   suspended then there is something wrong, you need to contact Salesforce support for help).


Check the debug logs:
            To capture the debug logs, you need to capture the "Traced Entity Type" under "Automated Process".

                                      You can find the source code HERE
Components Involved are :
Process builders :
PerformCustomLogicUsingPlatformEvent
PublishPlatformEvent
Trigger Names :
TriggeronCloud
AccountTriger
Platform Event name:
Name: Cloud News
API  : Cloud_News__e

                                   Special Thanks to @Sri Harsha Nandam.

tHiNk gooD and dO thE bEsT.........MANJU NATH 🌝

Comments