I have a batch job that implements Database.RaisesPlatformEvents and when a BatchApexErrorEvent is generated from the Batch class's execute() method I handle it in a BatchApexErrorEventTrigger. I would like to relate the BatchApexErrorEvent back to the Batch's job, but I've found:
- batchableContext.getJobId() != event.AsyncApexJobId. <-- how do I get this JobId from event???
- batchableContext.getChildJobId() == event.AsyncApexJobId
The scenario is that I create a custom log record in the start method and set the batchableContext.getJobId() on it. If there is an error in the execute method (could be uncatchable), the BatchApexErrorEventTrigger handles it and from there I need to relate back to that initial log record. I cannot, reliably, create and/or update the DB in the execute method to supply a child ID since an error could occur at any time in the execute() and an exception could roll back anyway.
Is there a way to get to the Id of batchableContext.getJobId() when handling a BatchApexErrorEvent?
I thought that the event.RequestId would work, but that ID is not a job Id. When I write it out it is writing records that start with 4XF.