Publish a Platform Event
In this last step, you implement a Chatter Apex trigger that triggers a notification for each post containing the #BearAlert
topic.
Implement a Chatter Apex Trigger
- Create an Apex trigger named
BearAlertTopicAssignmentTrigger
:- In Visual Studio Code, under force-app/main/default, right-click the triggers folder and select SFDX: Create Apex Trigger.
- Enter
BearAlertTopicAssignmentTrigger
for the name of the trigger. - Press Enter and then press Enter again to accept the default
force-app/main/default/triggers
.
- Replace the default code with this:
- The trigger intercepts all new Chatter posts that contain one or more topics.
- If a post contains the
#BearAlert
topic, the trigger publishes the post message as a notification through the standardEventBus
utility class. Errors are checked after publication.
Code Highlights:
- Save the file.
- Right click on the
triggers
folder and select SFDX: Deploy Source to Org to deploy your trigger to your org.
Test the Chatter Trigger
Now you can test your work. Create a Chatter post to ensure that it triggers a notification.
- From the App Launcher (
), find and select Sales, or if the Sales app is already open, refresh the browser.
- Click Chatter.
- Create a new post with the following content:
At this point, your message appears both as:
- A notification toast
- A Chatter post
Project Summary
Congratulations, you are now a platform events expert!
Now you know how to define a platform event, how to publish events with Apex, and how to subscribe to events via a Lightning component that uses the lightning/empApi
module.
You can apply your newly gained knowledge to build your own event-driven applications.