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 )


 

Tuesday, 21 July 2020

Integration With Salesforce Without using OAuth2.0




As we all know, we configure a connected app with OAuth2.0 to authenticate and authorize a safe visit for our clients. But what if our client does not support OAuth2.0? Is there any other way to authenticate our clients? The answer is Yes! and here is what you can do!

  1. Go to Setup and search for Sites. Now, Go to Sites and register your company's Salesforce site domain. 



  2. Now, you are ready to create a site for the domain you just registered. Click on New and create a new site. Activate the site after successful creation. 



  3. A user (SITENAME Site guest user) and a profile (SITENAME profile) get created after you create the site. Click on Public Access Settings to give object level and field level permission to Site Guest user for the corresponding profile. 

  1. What now? Nothing, You are already home!. You are all set to expose your services to your client. You just need to select one of the hash-based authentication methods which can be used in both the systems ( Salesforce and client system).

Let’s use HMAC SHA256 code to authenticate our clients. You can visit Here to more read about HMAC.

HMAC SHA256 uses 2 parameters to generate signature. We will use Secret key and Request body for authentication.

    • Use any secret Code which is shared between you and the client. Let’s say you decide to use ‘AuthenticateMe’.
    • So, here is how your client will generate an HMAC signature.

      EncodingUtil.base64Encode(crypto.generateMac('HmacSHA256',requestBody,’ AuthenticateMe’’)));

    • Ask your client to send a generated signature in Headers with a key such as ‘HMACSignature’( you can suggest them any key based on your requirement).
    • You will receive this header and compare it with the signature you created using the same secret key and body.




You just integrated with your client system without OAuth2.0. Do you know any other way to achieve this? Let me know in the comment below.