Friday, 21 July 2017

Custom Notifications with Platform Events Beta Winter'17 #Salesforce

 The platform provides an event-driven messaging architecture to enable apps to communicate inside and outside of Salesforce. Use platform events to deliver secure and scalable custom notifications within Salesforce or from external sources.

Platform Events uses event-driven (or message-driven) software architecture.
In the event driven architecture there are event consumers, event producers and channels.
Event Producers produce the event and the event are performed in real time by consumers, and the event passes through the channel. Multiple events can be produced and passed through the channel and consumed by consumers.

Here is diagram for more clearance, source Salesforce


Lets talk real now,  this Platform events can be used for sending event from salesforce or outside. For example like event of door opening and closing of a lift, and if it doesn't opens then create case.

So to handle these event in multpile ways and to process or execute these events, Salesforce has given many ways for example we can build these events using process builder and execute using flows. 
In the same way we can have apex triggers on Platform events to handle exceptions, there are few new methods have been provided by salesforce specifically for Platform Event.

Now its time for example, login to your salesforce and in quick find box, search for Platform Event
Click on new New Platform Event.

Here is one I've created 



Here I am giving an example of writing trigger and webservice on Platform Event and executing them in salesforce.

Below is the apex trigger on Platform Events, on After insert event which will check for the door open and closed value using the is_Closed__c field. if the door is closed then it will throw the exception, for exception in the Platform event, Salesforce has introduced Event.RetryableException is property to check how many times the trigger has been retried, if failed then you can perform other actions.

//Trigger on 
trigger EventTrigger on Open_Door__e ( after insert ) {

    for(Open_Door__e odEvent : Trigger.new) {
    
        //check for gate is open 
        if(odEvent.Is_Closed__c == false) {
            
            //Allowed to go in
            
        } else {
        
//exception to try event less then 4 times
            if (EventBus.TriggerContext.currentContext().retries < 4) {
            
                // Condition isn't met, so try again later.
                throw new EventBus.RetryableException( 'HOLD THE DOOR, HODORRRR.....!!');
            }
        }
    }
}



Now as we discussed the Platform events can be called or executed from outside salesforce using webservices, so here we will publish a new event using webservice, for publishing an event also we have a method called EventBus.publish().
Below we are publishing the event


global class DoorOpenAndClosedEvent {

    webService static void checkForDoors(Boolean isClosed) {

        //Check for closed door
        if(isClosed) {
        
            List<Open_Door__e> odEvent = new List<Open_Door__e>();
            odEvent.add(new Open_Door__e(Is_Closed__c=false, Working_Properly__c=true));
        
            //publish events
            List<Database.SaveResult> results = EventBus.publish(odEvent);
        
            // Inspect publishing result for each event
            for (Database.SaveResult sr : results) {
                for(Database.Error err : sr.getErrors()) {
                    System.debug('Error returned: ' +  err.getStatusCode() + ' - ' + err.getMessage());
                }
            }
        }   
    }     
}


Now execute the method in console
After running the method, go to the platform event where we have created, and down below we will see how it got published, by checking the below table, it show it give you status, published ID.


Status can be different,  Trigger states can be one of the following.



This is a small tutorial of using this newly delivered Platform Event. Soon will get to know the power of this object.

Key Points about Platform Events

  • A platform event is a special kind of Salesforce entity, similar in many ways to an sObject.
  • You cannot update or insert Platform Events.
  • event publishing is equivalent to a DML insert operation, DML limits and other Apex governor limits apply.
  • You can use any Salesforce API to create platform events, such as SOAP API, REST API, or Bulk API.

Important Links




Thanks

1 comment:

  1. Great post, it was nice to see this article. It was really appreciable. Thank you so much for sharing such an informative article. Checkout here more info about virtual conference platform

    ReplyDelete