Can’t Save Class (batch or future jobs pending or in progress)

I have a problem saving one of my classes into server. The error that I get is:

Error: Compile Error: This Apex class has batch or future jobs pending or in progress at line -1 column -1

I have tried everything mentioned in this article and none worked. (I even logged a case with Salesforce and they said we don’t support devs, ask your question online).

To be more specific, I tried the following scripts:

List<CronTrigger> listCronTrigger = [SELECT Id, CronExpression, EndTime, NextFireTime, OwnerId,
        PreviousFireTime, StartTime, State, TimesTriggered, TimeZoneSidKey FROM CronTrigger 
        WHERE State = 'Waiting' or State = 'Running'];

If (listCronTrigger.size() > 0) {
    for (Integer i = 0; i < listCronTrigger.size(); i++) { 
        System.abortJob(listCronTrigger[i].Id);
    }
}

also I tried running above script without the ‘WHERE’ condition to basically abort all the jobs. After running these scripts I don’t see any scheduled jobs in my org:

All Scheduled Jobs (empty)

Then I tried saving to server using IlluminatedCloud on IntelliJ, Mavens Mate on Atom and still I get the same error. Then I tried saving to server using developer console on my org, same error. Then I tried directly modifying it in Setup > Apex Classes > myClass.cls and still get the same error:

Apex Class save error

Update:
The following setting in Setup > Deployment Settings is enabled in my org:

Allow deployments of components when corresponding Apex jobs are
pending or in progress

Note that although I don’t see any scheduled jobs in my org, I figured that there are bunch of old queued apex jobs:

Apex Jobs (Queued)

I searched for solutions to kill these jobs, the only thing I found was this script:

for (AsyncApexJob aJob : [SELECT Id, Status, JobItemsProcessed, TotalJobItems, NumberOfErrors FROM AsyncApexJob Where Status = 'Queued']) {
    system.abortJob(aJob.Id);
}

But noticed that this script is supported up to version 39!

When I logged this case with Salesforce, I got this:

Support response

Answer

I finally found a hack to work around the issue and save my class. In short:

Delete that class from your org and then save it back to server using
your IDE.

More Details/Explanations:
Just to recap: there was a pending Apex job with no parent CronTrigger, so I couldn’t delete it using System.abortJob(). This is a Salesforce bug that happens when your scheduled job is delayed so much that it goes past its own execution time and then it remains there forever! If it’s not a dev org, SF will remove those jobs for you manually, if not, based on above research nothing basically works. Deleting the class will get rid of these jobs.

Attribution
Source : Link , Question Author : ashkanent , Answer Author : ashkanent

Leave a Comment