Newer Version Available

This content describes an older version of this product. View Latest

Example: Publish Callback Class That Creates Follow-Up Tasks for Failed Publishes

This publish callback class creates a task when event publishing fails in the onFailure method. The inserted task includes the number of failed events and the event UUIDs.
1public class FailureCallback implements EventBus.EventPublishFailureCallback {
2    
3    public void onFailure(EventBus.FailureResult result) {
4        List<String> eventUuids = result.getEventUuids();
5        System.debug(eventUuids.size() + ' events failed to publish.');      
6        System.debug('FailureCallback eventUuids to match with event objects: ' + eventUuids);
7
8        // Create a follow-up task
9	insertTask(eventUuids, false);
10    }
11    
12    private void insertTask(List<String> eventUuids, Boolean isSuccess) {
13        String eventIdString = '';
14        for (String evtId : eventUuids) {
15            eventIdString += evtId + ' ';
16        }
17        Task t = new Task();
18        if (isSuccess == false) {
19            t.Subject = 'Follow up on event publishing failures.';
20            t.Description = eventUuids.size() + 
21                ' events failed to publish. Event UUIDs: '
22            + eventIdString; 
23        }
24
25        // Set the due date
26        t.ActivityDate = Date.today().addDays(3);
27        // Set owner ID explicitly. 
28        // Otherwise, the task assignee is the Automated Process User.
29        // Change the user ID to a valid user ID in your org.
30        t.OwnerId = '005RM000002QhQ1YAK';
31        // Insert task
32        Database.SaveResult sr = Database.insert(t);
33        if (!sr.isSuccess()) {
34            for(Database.Error err : sr.getErrors()) {
35                System.debug('Error returned: ' +
36                             err.getStatusCode() +
37                             ' - ' +
38                             err.getMessage());
39            }
40        }
41    }
42}