Monday, April 22, 2019

Platform Events : Way to Deliver Custom Notifications within Salesforce or to external Applications

Salesforce provides Platform events which can be used create custom notification which can be used within Salesforce app or can be send to external applications. These notification are secure and scalable.

Platform events are part of Salesforce’s enterprise messaging platform. It utilizes publish and subscribe flow. One or many subscribe to same event and carry out different actions.

In lightning events, we define event and then create attributes through which information can shared between lightning components. Similar to this, you create platform event through point and click and then create custom fields which will be used to publish information by using platform events and subscriber can utilize this information.

Platform Events v/s Streaming API

  • In Streaming API (using Push Topics), subscriber receive notification based on changes or updates to records.
  • You can not edit or delete platform event records. You can only create or insert platform events.
  • Visualforce and Lightning component apps can subscribe to platform events using CometD similar to push topics.
  • You can not view platform event in Salesforce user interface and cannot query it through SOQL.
  • You can handle or subscribe platform events through triggers by using after insert.
  • Only "AfterInsert" event is available for platform event objects.
  • Platform events can be published by Process builder, Process Flow, apex, REST API by inserting an sObject.
  • Platform events persist for 24 hours only.
Permission for platform events is controlled through profile or permission sets.

Platform events can be used to publish information and has no dependency on existing salesforce records.

In order to create platform events, navigate to Setup --> Platform Events --> New Platform Event.

I have created a platform event "Demo Event" and going to use apex in order to publish event and subscribe this event through triggers.

When you create a platform event, the system appends the __e suffix to create the API name of the event.

How to fire/publish platform event
  • You have to create record in order to fire platform event.
To publish event messages, you create an instance of the event and pass it to the EventBus.publish method. Use below code snippet to publish event:

// Create an instance of the Demo event 
Demo_Event__e demoEvent = new Demo_Event__e(
           Event_Info__c='Demo event is fired using Apex', 
           Is_Event_Valid__c=true, 
           Event_Publisher__c='Apex Code');
// Call method to publish events
Database.SaveResult sr = EventBus.publish(demoEvent);
// Inspect publishing result 
if (sr.isSuccess()) {
    System.debug('Successfully published event.');
} else {
    for(Database.Error err : sr.getErrors()) {
        System.debug('Error returned: ' +
                     err.getStatusCode() +
                     ' - ' +
                     err.getMessage());
    }
}
  • You can create record using process builder or process flow.
  • You can create event using REST API. Below is details of HTTP Request:
Endpoint- /services/data/v45.0/sobjects/Demo_Event__e/
body
{
   "Event_Info__c" : "Demo event is fired using REST API CALL",
   "Is_Event_Valid__c" : true,
   "Event_Publisher__c" : "REST API CALL"
}
Method - POST
HTTP Response:

{   
   "id" : "e00xx0000000ccf",
   "success" : true,
   "errors" : [ ],
   "warnings" : [ ] 
}

How to Subscribe Platform Events using Apex Triggers

Whenever a record is created for platform event, after insert trigger gets fired. You can use below code to subscribe for platform event and perform logic as per your need:

trigger DemoEventTrigger  on Demo_Event__e (after insert) {
List<Task> taskList = new List<Task>();
for (Demo_Event__e event: Trigger.New) {
if (event.Is_Event_Valid__c == true) {
//perform logic based on event information
}
   }
}

Below is snapshot explaining Platform Events.
Hope this will help!!!




9 comments:

  1. Awesome explanation...it is really helpful.
    Thank you.

    ReplyDelete
  2. I am jovial you take pride in what you write. It makes you stand way out from many other writers that can not push high-quality content like you. sviluppo sito ecommerce

    ReplyDelete
  3. When you use a genuine service, you will be able to provide instructions, share materials and choose the formatting style. Kpop album buying and forwardingg

    ReplyDelete
  4. Thank you very much for writing such an interesting article on this topic. This has really made me think and I hope to read more. 3PL Amazon seller warehouse

    ReplyDelete
  5. To understand the understanding as well as discover of drug store service hospital pharmacy technician jobs quick, a history in chemistry, English, Biology, Chemistry as well as Health and wellness education and learning of any kind of kind might be useful.

    ReplyDelete
  6. What a fantabulous post this has been. Never seen this kind of useful post. I am grateful to you and expect more number of posts like these. See this link urgent care for fever in child.

    ReplyDelete
  7. Quadrobits gives itself as a premiere international virtual advertising and marketing organization. Here at https://quadrobits.com/. To make sure the first-rate healthy to your desires, behavior a comprehensive assessment, considering their track document, client comments, and enterprise alignment.

    ReplyDelete
  8. Hero MotoCorp, a respected Indian motorcycle manufacturer, has a wide selection of motorcycles available in Bangladesh. For the latest prices and model availability, it is recommended to visit their official website or contact local dealers. Visit Hero Bike Price in Bangladesh for more information.

    ReplyDelete