Introduction to Platform Events
Platform Events are a powerful feature of Salesforce that allow applications to communicate with each other in a loosely coupled, event-driven architecture. With Platform Events, applications can publish events when something of interest happens, and other applications can subscribe to those events to take action.
Platform Events are built on top of the Salesforce messaging infrastructure and are designed to work seamlessly with other Salesforce technologies like Apex, Lightning Components, and Visualforce. They are similar to custom objects in that they can be defined with fields and triggers, but they are optimized for high-volume event delivery and are designed to be consumed by external applications as well.
To Use Platform Event with Lightning Web Components:
Creating a Platform Event
Creating a Platform Event is a straightforward process. Here are the steps:
In Setup, navigate to Platform Events.
Click New Platform Event.
Enter a label for the event, such as "Account Updated" This would create API Name "Account_Updated__e".
Optionally, enter a plural label and a description for the event.
Once you have created a Platform Event, you can define the fields that will be included in the event message. You can add up to 100 custom fields to a Platform Event, including standard fields like CreatedById, CreatedDate, and LastModifiedById. You can also define validation rules for the event fields and write Apex triggers to process the events.
For this example I have created two custom fields Change_Type__c and RecordId__c.
Publishing a Platform Event
You can publish an event using Apex Trigger, Flow, Lightning Web Component or any Apex Code as well.
1. Publish Event Using LWC:
In this example, we are importing the createRecord method from the lightning/uiRecordApi module and the schema for our Platform Event (MY_EVENT_OBJECT). We are then defining the handleClick method, which will create and publish the event when called.
The createRecord method takes a single parameter, a record input object, which has the following properties:
apiName: The API name of the object to create a record for. For Platform Events, this should be the object API name of the Platform Event (MY_EVENT_OBJECT.objectApiName in this example).
fields: An object that maps field API names to their values. For Platform Events, you should include the fields that you defined for the event.
When the createRecord method is called, it will create a new record of the specified object type and publish it as a Platform Event. If the event is published successfully, the then callback will be called with the response, which will contain the ID of the new record. If there is an error publishing the event, the catch callback will be called with the error.
2. Publish event using Apex Trigger:
Subscribing to a Platform Event in an LWC
In addition to publishing a platform event, you can also subscribe to a platform event in LWC. This allows you to listen for platform events published by other components, and take action in response.
To subscribe to a platform event, you need to use the subscribe method from the lightning/messageService module. This method takes three parameters:
The message context object, which you can get using the @wire(MessageContext) decorator.
The message channel to subscribe to, which is the name of the platform event with '__c' replaced by '__e'.
A callback function that's called whenever a new platform event is received.
The callback function should take one parameter, which is the platform event object. You can access the fields of the platform event object using dot notation.