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
vagishvagish 

How to get a job id of future call?

Hi,

When we make future calls, a job gets enqueued in asyncapexjob record. Is there any way to get job id of future call which has been currently made?
For example, I have a button on case record, when I click that button, a future method runs and calls other web service. Now, multipe people can click on the button at the same time/or almost near by time so I want to display the status of their job.

Is it possible at all?

Thanks.
Best Answer chosen by vagish
logontokartiklogontokartik
you can query AsyncApexJob and also get the CreatedById field back. so you would know exactly which user created the job,

List<AsyncApexJob> futureCalls = [Select Id, CreatedById, CreatedBy.Name, ApexClassId,MethodName,Status from AsyncApexJob where JobType = 'future'];


All Answers

logontokartiklogontokartik
HI Vagish,

Please use AsyncApexjob object with a jobType of future

https://www.salesforce.com/us/developer/docs/api/Content/sforce_api_objects_asyncapexjob.htm

vagishvagish
Here is more elaborative example-
6 people clicked on the button which invokes future call, so 6 future calls will be made and 6 future jobs will be enqueued. 
Now, is it possible to show job status of the future call to their respective user only? I don't want to show all the future job status to all the users.
logontokartiklogontokartik
you can query AsyncApexJob and also get the CreatedById field back. so you would know exactly which user created the job,

List<AsyncApexJob> futureCalls = [Select Id, CreatedById, CreatedBy.Name, ApexClassId,MethodName,Status from AsyncApexJob where JobType = 'future'];


This was selected as the best answer
grangran
Try this .... 

From the click event of the button... call the following SOQL in the click event to find the future job Id invoked by the currently logged in user

List<AsyncApexJob> futureCalls = [Select Id, CreatedById, CreatedBy.Name, ApexClassId,MethodName,Status from AsyncApexJob where JobType = 'future' and Id=:UserInfo.getUserId() Sort By CreatedDate ASC]; 

and then loop through 'futureCalls' to pick up the Job Id of the latest future call invoked by the current user'

Cheers