Skip to main content
지금은 온라인 Trailblazer Community가 읽기 전용으로 제공됩니다. Trailblazer Community는 곧 Slack에서 제공되어 새로운 활동을 이어갈 예정입니다. FAQ 읽기 | 도움 받기

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

  1. Create an Apex trigger named BearAlertTopicAssignmentTrigger:
    1. In Web Console, under force-app/main/default, right-click the triggers folder and select SFDX: Create Apex Trigger.
    2. Enter BearAlertTopicAssignmentTrigger for the name of the trigger and press Enter.
    3. Select Notification__e Custom from the list and then press Enter.
    4. Update the list of checkboxes so that only after insert is checked then click OK.
  1. 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 : Trigger.new){
            if (ta.EntityId.getSObjectType().getDescribe().getName().equals('FeedItem')) {
                feedIds.add(ta.EntityId);
            }
        }
        // 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 :Trigger.new AND Topic.Name = 'BearAlert']) {
            messages.add(feedItems.get(ta.EntityId).body.stripHtmlTags().abbreviate(255));
        }
        // 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.

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 the lightning/empApi module.

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

Salesforce 도움말에서 Trailhead 피드백을 공유하세요.

Trailhead에 관한 여러분의 의견에 귀 기울이겠습니다. 이제 Salesforce 도움말 사이트에서 언제든지 새로운 피드백 양식을 작성할 수 있습니다.

자세히 알아보기 의견 공유하기