Salesforce platform events to deliver secure and scalable custom notifications within Salesforce or from external sources. Events get sent whether or not receivers are listening, and receivers don’t acknowledge it when they receive an event. It can be explained using following diagram

 

Salesforce Platform Events

 

As you can see from this architecture, Event Producer send events to event bus and event consumer listen for event, there can be a single producer or they can be multiple.

 

When to user Platform Events:

Platforms events are very useful when you want to communicate Salesforce with any external system or you want to communicate between two lightning applications or two any visualforce.

 

How to setup Platform event:

Creating a platform event is almost identical to creating a custom object. “Platform Event” is now a first-class object in Salesforce with the same strongly typed, versioned, and customizable schema you have come to expect from Salesforce’s metadata platform.

In Setup, use the quick find to locate the Platform Events node where you can start creating platform events as shown below:

Salesforce Platform event setup

 

How to Create Platform events using Apex:

 

List lstOpportunityEvent = new List();
//create single event object
Opportunity_Platform__e objEvent = new Opportunity_Platform__e();
objEvent.Stage__c = 'Closed';
lstOpportunityEvent.add(objEvent);
List<Database.SaveResult> results = EventBus.publish(lstOpportunityEvent);

Considerations:

  • The allOrNoneHeader API header is ignored when publishing platform events through the API.
  • The Apex setSavepoint() and rollback() Database methods aren’t supported with platform events.
  • Publishing platform events, DML limits and other Apex governor limits apply.
  • Only after insert triggers are supported for platform events because event notifications can’t be updated.
  • You can’t query event notifications using SOQL.