2

Is it possible to get Apex Job Id in Apex job after executing the future call?

Apex Job List

Basically I want to get the job Id and its detail information to copy it to another custom object. So after the future calls/any apex job is running, it will also create a record to the custom object

How can I achieve this?

2 Answers 2

3

The job id is returned when the job is created e.g.:

Id jobID = System.enqueueJob(...);

or

Id batchprocessid = Database.executeBatch(...);

or from the context parameter passed into the asynchronous code being run. Once you have the Id, you can query for more information:

AsyncApexJob jobInfo = [
        SELECT Status, NumberOfErrors
        FROM AsyncApexJob
        WHERE Id = :jobID
        ];

and see e.g. AsyncApexJob for the available data fields.

The work of "create a record to the custom object" based on the AsyncApexJob fields will have to be done within the future call itself (because that could run any time in the future and so cannot pass data back to the caller or be guaranteed to have even started at any point in time) or in whatever other code that launches the job. Or the job itself could do the work at the start or end.

1

Sorry but you can't. The only way is you need to query job based on what Class and Method is called in that job. for example :

SELECT Id  FROM AsyncApexJob where MethodName='method_name' And ApexClassId='apex_Id' order by createddate desc

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .