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);
   }
}
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);
   }
}

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.