Publish a Platform Event

What You’ll Do

In this step, you implement a Chatter Apex trigger that triggers a notification for each post containing the #BearAlert topic.

You will:

  • Implement a Chatter Apex trigger that publishes a platform event.

  • Test your work.

Follow Along with Trail Together

Want to follow along with an instructor as you work through this step? Take a look at this video, part of the Trail Together series on Trailhead Live.

(This clip starts at the 29:25 minute mark, in case you want to rewind and watch the beginning of the step again.)

Implement a Chatter Apex Trigger

  1. In the Developer Console, click File > New > Apex Trigger.

  2. Name the trigger BearAlertTopicAssignmentTrigger.

  3. Select the TopicAssignment sObject.

  4. Click Submit.

  5. Replace the default code with this:

trigger BearAlertTopicAssignmentTrigger on TopicAssignment (after insert) {
    // Get FeedItem posts only
    Set<Id> feedIds = new Set<Id>();
    for (TopicAssignment ta :{
        if (ta.EntityId.getSObjectType().getDescribe().getName().equals('FeedItem')) {

    // Load FeedItem bodies
    Map<Id,FeedItem> feedItems = new Map<Id,FeedItem>([SELECT Body FROM FeedItem WHERE Id IN :feedIds]);

    // Create messages for each FeedItem that contains the BearAlert topic
    List<String> messages = new List<String>();
    for (TopicAssignment ta : [SELECT Id, EntityId, Topic.Name FROM TopicAssignment WHERE Id IN AND Topic.Name = 'BearAlert']) {

    // Publish messages as notifications
    List<Notification__e> notifications = new List<Notification__e>();
    for (String message: messages) {
        notifications.add(new Notification__e(Message__c = message));
    List<Database.SaveResult> results = EventBus.publish(notifications);

    // Inspect publishing results
    for (Database.SaveResult result : results) {
        if (!result.isSuccess()) {
            for (Database.Error error : result.getErrors()) {
                System.debug('Error returned: ' + error.getStatusCode() +' - '+ error.getMessage());

Code Highlights:

  • 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 standard EventBus utility class. Errors are checked after publication.

  1. Save the file and close the Developer Console.

Test the Chatter Trigger

Now you can test your work. Create a Chatter post to ensure that it triggers a notification.

  1. From the App Launcher (App launcher icon), find and select Sales, or if the Sales app is already open, refresh the browser.

  2. Click Chatter.

  3. Create a new post with the following content:

    #BearAlert False alarm: It's just a big dog!

At this point, your message appears both as:

  • A notification toast
  • A Chatter post

An image showing a notification being triggered by 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 CometD.

You can apply your newly gained knowledge to build your own event-driven applications.

Keep learning for
Sign up for an account to continue.
What’s in it for you?
  • Get personalized recommendations for your career goals
  • Practice your skills with hands-on challenges and quizzes
  • Track and share your progress with employers
  • Connect to mentorship and career opportunities