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: ' +
7 eventUuids);
8
9 // Create a follow-up task
10 insertTask(eventUuids, false);
11 }
12
13 private void insertTask(List<String> eventUuids, Boolean isSuccess) {
14 String eventIdString = '';
15 for (String evtId : eventUuids) {
16 eventIdString += evtId + ' ';
17 }
18 Task t = new Task();
19 if (isSuccess == false) {
20 t.Subject = 'Follow up on event publishing failures.';
21 t.Description = eventUuids.size() +
22 ' events failed to publish. Event UUIDs: '
23 + eventIdString;
24 }
25
26 // Set the due date
27 t.ActivityDate = Date.today().addDays(3);
28 // Set owner ID explicitly.
29 // Otherwise, the task assignee is the Automated Process User.
30 // Change the user ID to a valid user ID in your org.
31 t.OwnerId = '005RM000002QhQ1YAK';
32 // Insert task
33 Database.SaveResult sr = Database.insert(t);
34 if (!sr.isSuccess()) {
35 for(Database.Error err : sr.getErrors()) {
36 System.debug('Error returned: ' +
37 err.getStatusCode() +
38 ' - ' +
39 err.getMessage());
40 }
41 }
42 }
43}