0

Is it possible to get the ApexJobId within the Future method apex code (or from the method its invoked from)?

Use case - Our application is invoking multiple same future calls from within one apex transaction. I want to capture the JobId of each call and track the progress. I am using a helper custom object to store the call details along with the record it's processing on.

Challenge - Just making an AsyncApexJob SOQL will retrieve all jobs and but I can't relate which job was for which record.

Alternate Solution - Should I be using Queueable Apex interface instead?

2

Is it possible to get the ApexJobId within the Future method apex code (or from the method its invoked from)?

No. Future was designed in response to Batchable's excessive resource usage and cumbersome interface to be a quick "fire and forget" type mechanism. Unfortunately, this means that you can't specifically link any given future call to a Job ID reliably in Apex Code. People have historically come up with semi-reliable workarounds in some cases, but they really were not meant to be used this way.

Alternate Solution - Should I be using Queueable Apex interface instead?

Yes, you should. Unlike future methods, Queueable jobs were meant to be comparable in power to Batchable, but use fewer resources in exchange for some limits on how quickly they can execute and how much data they can work with. While future methods will work for the foreseeable future, you should consider Queueable in most general use cases where future was previously used, including asynchronous callouts and data processing.

0

Queueable Apex interface is the solution for my use case.

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.