Salesforce platform events are used to deliver secure and scalable custom notifications from salesforce to external systems. Platform events are also used to communicate within salesforce and also between lightning applications.
Events are sent independent of the recievers listening to those events. I have explained the events using the diagram below:
As you can see above, Event producer send events to event bus and event consumer listens for the event. There can be single producer or also multiple producers.
When to use platform events?
- Platform events are very useful when you want to communicate Salesforce with any external system.
- Platform events are also used to communicate between two lightning applications.
- Platform events are very useful when you want to communicate between two visualforce pages.
How to setup platform event?
Creating of platform event is very similar to the creation of custom object in salesforce. Platform event is a first-class object with the same strongly typed, versioned and customizable schema similar to Salesforce metadata platform.
Go to setup in salesforce and look for platform events and click the link “Platform Events” and start creating your platform events.
How to create Platform Events using Apex?
//create a list List listOfOpporEvents=new List(); //create a single event object Opportunity_Platform__e objEvent=new Opportunity_Platform__e (); objEvent.Stage__c='Open'; //add opportunity event to list listOfOpporEvents.add(objEvent); List<Database.SaveResult> output=EventBus.publish(listOfOpporEvents);
Things to Note regarding Platform Events:
- Whenever you are publishing platform events, Governor limits and DML limits are also applied.
- You can only use after insert trigger for platform events because you can not update event notifications.
- You can not query event notifications using SOQL.
To learn more about these platform events. Click the link here