function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Abhijit Shrikhande 10Abhijit Shrikhande 10 

How can I get the ApexJobId of the instance?

I have an Queueable class.
Example:
public class AsyncExecutionExample implements Queueable {
    public void execute(QueueableContext context) {
        Account a = new Account(Name='Acme',Phone='(415) 555-1212');
        insert a;        
    }
}
I am calling this class from another class.
Example:
ID jobID = System.enqueueJob(new AsyncExecutionExample());

In my Queueable class, I plan to send an email to certain users in case the process falters or fails along the way. I need to get the ApexJobId of the running process for that. How can I get the ApexJobId from within the queueable class?
 
Best Answer chosen by Abhijit Shrikhande 10
Abhijit Shrikhande 10Abhijit Shrikhande 10
I just found out that the QueueableContext object lets you do this. Here is my code.
 
public void execute(QueueableContext context) 
    {
            string jobId;
            jobId= context.getJobId();
System.Debug(jobId);

}

 

All Answers

pconpcon
I don't know of a good way to do it other than to query the AsyncApexJob object and try to find one that is currently running that matches your AsyncExecutionExample class.
Abhijit Shrikhande 10Abhijit Shrikhande 10
I just found out that the QueueableContext object lets you do this. Here is my code.
 
public void execute(QueueableContext context) 
    {
            string jobId;
            jobId= context.getJobId();
System.Debug(jobId);

}

 
This was selected as the best answer
pconpcon
Good to know.  I don't know why I completely glossed over the fact that you had the QueueableContext object.  I'll blame lack of sleep.