3

We have an Apex class that runs nightly at 1am (implements Schedulable). The class queries all records where the NextDate field is <= today. For all those records, it then updates the field to another date in the record. See code below

A couple days ago the class threw an error because one of the records violated a validation rule. No problem, fixed the record and expected the class to re-update all the records that were skipped the because of the thrown record (that's why we use the <= in the query) on the following night.

Surprisingly, however, all the records that were skipped are not updating in future jobs (its been several days now).

Thoughts on why this is happening and how to fix it? I suspect that SF has somehow marked these records with some kind of "error" marking and will not update them again in future runs. Is that right? if so, how do we "unmark" them?

global class cls_update_next_Date implements Schedulable
{
 global void execute(SchedulableContext ctx) 
  {
    list<contact> conlist = new list<contact>();
    list<contact> updateconlist = new list<contact>();
    conlist.clear();
    updateconlist.clear();
    conlist = [select id,Status__c,NextDate__c,Next_Date_Calculation__c from Contact where NextDate__c <=:date.Today() and Status__c = 'Enrolled'];
    for(contact con:conlist)
    {
        con.NextDate__c = con.Next_Date_Calculation__c;
        con.TriggerNextDateEmail__c = false; //need to reset this so email under time based workflow trigger is sent
        updateconlist.add(con);            
    }
    if(updateconlist.size()>0)
    {
        update updateconlist;
    }

    // TriggerNextDateEmail__c has been set to false, now need to reset it to true so the email is re-triggered
    updateconlist.clear();
    for(contact con:conlist)
    {
        con.TriggerNextDateEmail__c = true;
        updateconlist.add(con);            
    }
    if(updateconlist.size()>0)
    {
        update updateconlist;
    }
 }
}
4

I have not witnessed this behavior. If a job of mine ever fails, it's always successfully processed in the next scheduled run.

Have you queried the database to ensure there are records to process? If there are records, is your job still executing nightly? Is it reporting any errors? Is it possible that the records which have accumulated due to the failure exceed the limit and you need to implement batch apex? I recommend you manually run the code as anonymous Apex on a small sample of the queried records, limited to five or so.

Going forward, consider implementing batch Apex to processes smaller batches of these records. That way if one batch fails, you may still see partial success.

  • thanks for the suggestions. We didn't have that many records to go through, so we just went through by hand, but your idea of splitting into multiple queries would have been a good one if we had a large data set. – StartingAgain Nov 17 '14 at 17:12
0

Figured it out. In a batch job salesforce will report the first record with an error and only the first record. And then in subsequent runs, it will NOT report the other records that had errors in the first run - you have to go find those yourself.

So, what we did was go in and fix the record that was throwing an error, but there were subsequent records that had errors (it was a validation rule that we weren't reporting against) that we didn't see in any error reports and that we weren't able to see in our testing because it was an obscure validation rule. So we had to go record by record to figure out which one it was.

  • StartingAgain - good detective work. In the future, you can avoid these issues by using the Database.update(sobjectList,allorNothing) syntax with the second argument being false. Then, your code collects up all the errors in a stateful class instance variable and then in the finish() method, either emails the sysad all the erroneous records or uses dml to create a log entry in some custom DiagLog__c Sobject. This way, your code will successfully update all records that pass validations and log all records that fail. – cropredy Nov 17 '14 at 20:53
  • @crop1645 thanks, sounds useful. can you provide some sample code in this context? – StartingAgain Nov 18 '14 at 22:00
  • Take a look at this example - salesforce.stackexchange.com/a/34208/2602 . You can adapt as needed – cropredy Nov 18 '14 at 22:32

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.