例: 公開が失敗した場合と成功した場合にフォローアップ ToDo を作成する公開コールバッククラス
この公開コールバッククラスは、前述の例を変更したものです。EventBus.EventPublishSuccessCallback インターフェースも実装しており、成功したケースと失敗したケースの両方を処理します。イベント公開が失敗または成功したときに ToDo を作成します。この挿入される ToDo には、失敗したイベントの数とイベントの UUID が含まれています。
この例を実行する前に、例に指定されているメールアドレスを、組織で ToDo 作成の権限を持つユーザーのメールアドレスに変更します。FailureCallback クラスのデバッグログを表示するには、自動化プロセスユーザーのユーザー追跡フラグが設定されていることを確認します。詳細は、「コールバックの実行ユーザーとデバッグログ」を参照してください。
1public class FailureAndSuccessCallback implements EventBus.EventPublishFailureCallback,
2 EventBus.EventPublishSuccessCallback {
3
4 public void onFailure(EventBus.FailureResult result) {
5 List<String> eventUuids = result.getEventUuids();
6 System.debug(eventUuids.size() + ' events failed to publish.');
7 System.debug('Callback eventUuids to match with event objects: ' + eventUuids);
8
9 // Create a follow-up task for failed events.
10 insertTask(eventUuids, false);
11 }
12
13 public void onSuccess(EventBus.SuccessResult result) {
14 List<String> eventUuids = result.getEventUuids();
15 System.debug(eventUuids.size() + ' events were published successfully.');
16 System.debug('Callback eventUuids to match with event objects: ' + eventUuids);
17
18 // Create a follow-up task for successful events.
19 insertTask(eventUuids, true);
20 }
21
22 private void insertTask(List<String> eventUuids, Boolean isSuccess) {
23 String eventIdString = '';
24 for (String evtId : eventUuids) {
25 eventIdString += evtId + ' ';
26 }
27 Task t = new Task();
28 if (isSuccess == true) {
29 t.Subject = 'Follow up on successful event publishing.';
30 t.Description = eventUuids.size() +
31 ' events published successfully. Event UUIDs: '
32 + eventIdString;
33 } else {
34 t.Subject = 'Follow up on event publishing failures.';
35 t.Description = eventUuids.size() +
36 ' events failed to publish. Event UUIDs: '
37 + eventIdString;
38 }
39
40 // Set the due date
41 t.ActivityDate = Date.today().addDays(3);
42 // Set owner ID explicitly.
43 // Otherwise, the task assignee is the Automated Process User.
44 // ---
45 // CHANGE EMAIL ADDRESS to the email of a valid user in your org.
46 // ---
47 User myUser = [SELECT Id from User WHERE Email='user@example.com'];
48 t.OwnerId = myUser.Id;
49 // Insert task
50 Database.SaveResult sr = Database.insert(t);
51 if (!sr.isSuccess()) {
52 for(Database.Error err : sr.getErrors()) {
53 System.debug('Error returned: ' +
54 err.getStatusCode() +
55 ' - ' +
56 err.getMessage());
57 }
58 }
59 }
60}コールバッククラスでイベントを公開するには、「例: コールバックインスタンスを使用したイベントの公開」に示されているコードスニペットを使用して、EventBus.publish メソッドのコールバックインスタンス名を FailureAndSuccessCallback に変更します。
1// Publish events with an instance of the callback.
2List<Database.SaveResult> results = EventBus.publish(eventList,
3 new FailureAndSuccessCallback());