Skip to content

Commit

Permalink
feat: best practice for platform event publish callback
Browse files Browse the repository at this point in the history
  • Loading branch information
pozil committed Jun 28, 2024
1 parent 1391e1a commit ee106ee
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
*/
public with sharing class PlatformEventPublishCallback implements EventBus.EventPublishFailureCallback, EventBus.EventPublishSuccessCallback {
/**
* Map that correlates event UUIDs with events
* Map that correlates event UUIDs with event data
*/
private Map<String, Event_Recipes_Demo__e> eventMap;
private Map<String, EventInfo> eventMap;

public PlatformEventPublishCallback(List<Event_Recipes_Demo__e> events) {
this.eventMap = new Map<String, Event_Recipes_Demo__e>();
for (Event_Recipes_Demo__e event : events) {
this.eventMap.put(event.EventUuid, event);
public PlatformEventPublishCallback(List<EventInfo> eventInfos) {
this.eventMap = new Map<String, EventInfo>();
for (EventInfo eventInfo : eventInfos) {
this.eventMap.put(eventInfo.EventUuid, eventInfo);
}
}

/**
* Callback for events that failed to publish
* Note: this method is always called by the automation user
* Note: this method is always called by the Automation user
*/
public void onFailure(EventBus.FailureResult result) {
// Get event UUIDs from the result
Expand All @@ -30,7 +30,7 @@ public with sharing class PlatformEventPublishCallback implements EventBus.Event

/**
* Callback for events that were successfully published
* Note: this method is always called by the automation user
* Note: this method is always called by the Automation user
*/
public void onSuccess(EventBus.SuccessResult result) {
// Get event UUIDs from the result
Expand All @@ -44,8 +44,8 @@ public with sharing class PlatformEventPublishCallback implements EventBus.Event
// Load accounts related to events
Set<Id> relatedAccountIds = new Set<Id>();
for (String eventUuid : eventUuids) {
Event_Recipes_Demo__e originalEvent = this.eventMap.get(eventUuid);
relatedAccountIds.add(originalEvent.AccountId__c);
EventInfo eventInfo = this.eventMap.get(eventUuid);
relatedAccountIds.add(eventInfo.accountId);
}
Map<Id, Account> relatedAccounts = new Map<Id, Account>(
[
Expand All @@ -60,11 +60,11 @@ public with sharing class PlatformEventPublishCallback implements EventBus.Event
List<Task> tasks = new List<Task>();
for (String eventUuid : eventUuids) {
// Retrieve event data
Event_Recipes_Demo__e originalEvent = this.eventMap.get(eventUuid);
EventInfo eventInfo = this.eventMap.get(eventUuid);

// Create a task on the related account
Task t = new Task();
t.WhatId = originalEvent.AccountId__c;
t.WhatId = eventInfo.accountId;
t.ActivityDate = Date.today().addDays(1);
if (isSuccess == true) {
t.Subject = 'Follow up on successful event publishing.';
Expand All @@ -75,9 +75,23 @@ public with sharing class PlatformEventPublishCallback implements EventBus.Event
t.Description =
'Events failed to publish. Event UUID: ' + eventUuid;
}
t.OwnerId = relatedAccounts.get(originalEvent.AccountId__c).OwnerId;
t.OwnerId = relatedAccounts.get(eventInfo.accountId).OwnerId;
tasks.add(t);
}
insert as system tasks;
}

/**
* Data object that holds the minimum amount of information to identify our event and potentially republish it.
* We recommend that you don't store all event fields to avoid hitting callback handler internal limits.
*/
public class EventInfo {
public String eventUuid;
public Id accountId;

public EventInfo(String eventUuid, Id accountId) {
this.eventUuid = eventUuid;
this.accountId = accountId;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,12 @@ public with sharing class PlatformEventRecipes {
);
}
if (CanTheUser.create(event)) {
PlatformEventPublishCallback.EventInfo eventInfo = new PlatformEventPublishCallback.EventInfo(
event.EventUuid,
event.AccountId__c
);
PlatformEventPublishCallback callback = new PlatformEventPublishCallback(
new List<Event_Recipes_Demo__e>{ event }
new List<PlatformEventPublishCallback.EventInfo>{ eventInfo }
);
return EventBus.publish(event, callback);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,24 +16,44 @@ Demonstrates how to write Platform Event publish success and failure callbacks
**See** [PlatformEventRecipes](https://github.com/trailheadapps/apex-recipes/wiki/PlatformEventRecipes)

## Constructors
### `public PlatformEventPublishCallback(List<Event_Recipes_Demo__e> events)`
### `public PlatformEventPublishCallback(List<EventInfo> eventInfos)`
---
## Fields

### `private eventMap``Map<String,Event_Recipes_Demo__e>`
### `private eventMap``Map<String,EventInfo>`


Map that correlates event UUIDs with events
Map that correlates event UUIDs with event data

---
## Methods
### `public void onFailure(EventBus result)`

Callback for events that failed to publish Note: this method is always called by the automation user
Callback for events that failed to publish Note: this method is always called by the Automation user

### `public void onSuccess(EventBus result)`

Callback for events that were successfully published Note: this method is always called by the automation user
Callback for events that were successfully published Note: this method is always called by the Automation user

### `private void insertTask(List<String> eventUuids, Boolean isSuccess)`
---
## Classes
### EventInfo

Data object that holds the minimum amount of information to identify our event and potentially republish it.
We recommend that you don't store all event fields to avoid hitting callback handler internal limits.

#### Constructors
##### `public EventInfo(String eventUuid, Id accountId)`
---
#### Fields

##### `public eventUuid``String`


##### `public accountId``Id`


---

---

0 comments on commit ee106ee

Please sign in to comment.