+ Start a Discussion
ms Scottms Scott 

Schedulable Apex class only running once from scheduler - scheduled to run every week, it runs once and won't repeat

I have a schedulable apex process that i use to update a date field and then update all of the records. The process works great, i use the scheduler and schedule it to run every week on Tuesday's at 10pm (for 2 months or longer). It fires on Tuesday, but won't fire the 2nd week or any other time. I tried every other day, every day, no matter how many time i schedule to process...it will only fire once. Is there's something i'm missing?

Here's my code;

global class SchedulerToUpdateDate implements Schedulable {
List<Grad_Employment_Detail__c> allRec = new List<Grad_Employment_Detail__c>();
List<Grad_Employment_Detail__c> toUpdate = new List<Grad_Employment_Detail__c>();
    global void  execute(SchedulableContext sc){
        allRec = [select id, current_Date__c from Grad_Employment_Detail__c];
        for(Grad_Employment_Detail__c ge: allRec){
            ge.current_Date__c = date.today();
            toUpdate.add(ge);
        }
        update toUpdate;
    }
}

Thanks in Advance! (this is my first apex class)

M Scott
 
Best Answer chosen by ms Scott
Glyn Anderson 3Glyn Anderson 3
I think that because you create the lists upon construction (i.e. at the time the class is scheduled), the second time it runs, the "toUpdate" list will contain repeated records - the records from the first run and the same records again.  This will cause a DML exception, because you can't update the same record more than once in a single update.  Try the version below.  It does not persist any data between executions.

<pre>
global class SchedulerToUpdateDate implements Schedulable
{
    global void  execute(SchedulableContext sc)
    {
        List<Grad_Employment_Detail__c> allRec = 
            [select id, current_Date__c from Grad_Employment_Detail__c];

        for ( Grad_Employment_Detail__c ge : allRec )
        {
            ge.current_Date__c = date.today();
        }
        update allRec;
    }
}
</pre>

All Answers

Glyn Anderson 3Glyn Anderson 3
I think that because you create the lists upon construction (i.e. at the time the class is scheduled), the second time it runs, the "toUpdate" list will contain repeated records - the records from the first run and the same records again.  This will cause a DML exception, because you can't update the same record more than once in a single update.  Try the version below.  It does not persist any data between executions.

<pre>
global class SchedulerToUpdateDate implements Schedulable
{
    global void  execute(SchedulableContext sc)
    {
        List<Grad_Employment_Detail__c> allRec = 
            [select id, current_Date__c from Grad_Employment_Detail__c];

        for ( Grad_Employment_Detail__c ge : allRec )
        {
            ge.current_Date__c = date.today();
        }
        update allRec;
    }
}
</pre>
This was selected as the best answer
PawanKumarPawanKumar
Please got to setup->apex classes->scheduled Apex then set your schdular to run every week.

User-added image

Regards,
Pawan Kumar
Glyn Anderson 3Glyn Anderson 3
ms Scott,  Did any of the answers solve your problem?  If so, please mark this question "Solved".  If not, let us know!
ms Scottms Scott
Sorry for the delay. Yes yes yes...modified the test code for it, pushed into production...and it's working like a charm! Thanks a bunch!...now where do I send the check for $3mil to?..(smile)

Michael Scott