0

We are trying to build some logic to track whether all high-volume published events have been successfully delivered to subscribers, and catch those that were not delivered for some reason to republish later on.

The idea is quite simple here: every time when we publish platform even, we will create a record in our Log object with json-serialized event data, event name and other info like that to be able to republish it if needed (probably manually at first stage). Then in each event subscriber trigger we will remove corresponding log record (or update status) for each received platform event.

The problem here is: how to match uniquely event received in subscriber trigger to record in Log table?

The standard approach like

create CorrelationId__c field in each event and generate some uuid before publishing

does not work for us, since we've already reached a patch flow for our managed package, i.e. SF does not allow to create new fields at this stage at all. That's why we are relying on our existent Log object and kind of 're-purposing' some of its existent fields for platform event.

So far we've found a method in 48.0 API which actually returns platform event uuid right after publishing from Apex based on Database.SaveResults:

Database.SaveResult publishResult = 
    EventBus.publish(
        eventToPublish
    )
;

String uuid = EventBus.getOperationId(publishResult);

The challenge here is how to get this uuid on receiving side, i.e. in subscriber trigger, purely from platform event or using system classes (again keeping in mind that we cannot create new field).

Or maybe there is some other way how we can generate our own UUID and recreate it on receiving side to use as a correlation id, but without passing it via event payload directly?

Any help would be appreciated.

3
  • 1
    By default the ReplayId info is sent as part of the payload. Maybe you could use that? Aside, I was wondering how you would handle scenario if you have multiple subscribers to the platform event? Jun 25, 2020 at 13:26
  • @HengkyIlawan Unfortunately, ReplyId is not available when we are publishing event to log it as a key, i.e. only subscriber trigger has access to ReplyId as a part of event payload. In current package version we have only one subscriber per each event type, so it's not a problem for now, but will be in the future.
    – wesaw
    Jun 25, 2020 at 23:10
  • You can have a trigger in the platform event and I believe you can get the replayId via the trigger? Jun 25, 2020 at 23:35

1 Answer 1

1

ReplayId is sent as payload to subscribers. You can write a trigger for the platform event and get the replayId, then do the log insertion.

trigger MyPETrigger on MyPE__e (after insert) {

    for (MyPE__e ev : trigger.new) {
        System.debug(ev.ReplayId);
    }

}
4
  • We need to have access to replyId on both sides, i.e. when submitting event (to put replyId in Log record) and when processing event in subscriber trigger (to be able to get appropriate record from Log table using replyId as one of criterion).
    – wesaw
    Jun 26, 2020 at 7:21
  • Actually my example was for the event publisher side, so you can use a trigger as an internal subscriber. Upon receiving a platform event message, create the log with the replayId. Jun 26, 2020 at 17:11
  • We are in a patch flow already, i.e. no new SF artifacts allowed, including fields and triggers)
    – wesaw
    Jun 26, 2020 at 19:48
  • Ah Ok! I learned new thing. Thanks. Jun 27, 2020 at 0:25

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .