6

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. 4XFToiUSPaAR3ykCac.

1
  • According to the documentation AsyncApexJobId is the "AsyncApexJob record for the batch Apex job that fired this event". It seems your findings contradict this. – Phil W Jul 22 '20 at 17:22
5

If you are getting back the child job ID, why don't you obtain the parent job ID this way:

SELECT ParentJobId FROM AsyncApexJob WHERE Id = :event.AsyncApexJobId

(Clearly you would want to actually bulkify this in real life)

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.