top of page

Platform Event with Lightning Web Components

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:

  1. In Setup, navigate to Platform Events.

  2. Click New Platform Event.

  3. Enter a label for the event, such as "Account Updated" This would create API Name "Account_Updated__e".

  4. Optionally, enter a plural label and a description for the event.

  5. Click Save.

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.


OR


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:

  1. The message context object, which you can get using the @wire(MessageContext) decorator.

  2. The message channel to subscribe to, which is the name of the platform event with '__c' replaced by '__e'.

  3. 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.


HTML

Javascript


Output:


Platform event using LWC
Output


2 Comments


Lohith Must
Lohith Must
4 days ago

Getting You don't have access to this record. Ask your administrator for help or to request access.

when trying to create event in lwc Js...I checked the profile permission as well .. all looks good..Any Thoughts?

Like

Sridhar NareshIT
Sridhar NareshIT
Sep 02, 2023

Very useful blog , Keep posting more information on LWC .


Like
bottom of page