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:
Platform Events in salesforce
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?

  1. Platform events are very useful when you want to communicate Salesforce with any external system.
  2. Platform events are also used to communicate between two lightning applications.
  3. 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.
Create a new platform event

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 ();
//add opportunity event to list
List<Database.SaveResult> output=EventBus.publish(listOfOpporEvents);

Things to Note regarding Platform Events:

  1. Whenever you are publishing platform events, Governor limits and DML limits are also applied.
  2. You can only use after insert trigger for platform events because you can not update event notifications.
  3. You can not query event notifications using SOQL.

To learn more about these platform events. Click the link here


Recent Posts