4

How do I delete Apex job submitted by a particular user? Below is what I found online but this script will delete all apex job with status = queued.

Thanks in advance.

   //ApexClass apxClass = [SELECT Id FROM ApexClass WHERE Name = 'myScriptName'];
//List<AsyncApexJob> lstJobs = [SELECT Id FROM AsyncApexJob WHERE Status = 'Queued' AND ApexClassId = :apxClass.Id];

List<AsyncApexJob> lstJobs = [SELECT Id FROM AsyncApexJob WHERE Status = 'Queued'];

for(AsyncApexJob job : lstJobs) {
   try {
      System.abortJob(job.Id);
   } catch(Exception ex ) {
      System.debug(ex);
   }
}

2 Answers 2

5

The CreatedById is the submitter of the job. You can filter on this field just as any other filterable field:

AsyncApexJob[] jobsFromUser = [SELECT Id FROM AsyncApexJob WHERE Status = 'Queued' AND CreatedById = :selectedUserId];
0
1
List<AsyncApexJob> lstJobs = [SELECT Id FROM AsyncApexJob WHERE Status = 'Queued' AND CreatedById = : userId];
for(AsyncApexJob job : lstJobs) {
   try {
      System.abortJob(job.Id);
   } catch(Exception ex ) {
      System.debug(ex);
   }
}

You must log in to answer this question.

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