1

I have a custom object 'chargent order', and a field 'todays date'. The classes save and they schedule and run, but nothing updates, and I can't figure out why.


global class ChargentDailyUpdate {
    global static void checkUpdate(ChargentOrders__ChargentOrder__c[] objects){
        for(ChargentOrders__ChargentOrder__c  obj: objects){
            obj.Todays_Date__c= date.today();
        }
    }
}



global class ChargentSchedulable implements Schedulable {
   global void execute(SchedulableContext SC) {
      ChargentDailyUpdate CDU = new ChargentDailyUpdate(); 
   }
}

I also would appreciate any advice on whether there are better ways to do this, and if this is the way should I do a batch job like 20 at a time (there are about 1000 records and workflows that fire off the 'todays date' changing). I think this method does all 1000 at the same time but I'm not certain, I don't have much apex experience. Thanks for any and all help.

2 Answers 2

3

It looks like you didn't actually call the method you needed to call. More to the point, you probably wanted a batchable class in case there's too many records to process.

global class ChargentSchedulable implements Schedulable, Database.Batchable<SObject> {
   global void execute(SchedulableContext SC) {
      Database.executeBatch(this);
   }
   global Database.QueryLocator start(Database.BatchableContext context) {
     return Database.getQueryLocator(
       [SELECT Id FROM ChargentOrders__ChargentOrder__c]
     );
   }
   global void execute(Database.BatchableContext context, ChargentOrders__ChargentOrder__c[] scope) {
     for(ChargentOrders__ChargentOrder__c record: scope) {
       record.Todays_Date__c = Date.today();
     }
     update scope;
  }
  global void finish(Database.BatchableContext context) {
  // Required, but nothing to do
  }
}
3
  • you missed the update statement in execute method. Correct me if i am wrong
    – sunny
    Commented Apr 5, 2017 at 4:00
  • You also forgot to implement the Batchable interface.
    – Adrian Larson
    Commented Apr 5, 2017 at 4:03
  • @AdrianLarson Ah hahaha. Not my finest moment, apparently.
    – sfdcfox
    Commented Apr 5, 2017 at 4:05
1

I can see you are missing the update statement.

Try below code:

global class ChargentDailyUpdate {
    global static void checkUpdate(ChargentOrders__ChargentOrder__c[] objects){
        for(ChargentOrders__ChargentOrder__c  obj: objects){
            obj.Todays_Date__c= date.today();
        }
       Update objects;
    }
}

global class ChargentSchedulable implements Schedulable {
   global void execute(SchedulableContext SC) {
      List<ChargentOrders__ChargentOrder__c> objects = [SELECT Id FROM ChargentOrders__ChargentOrder__c]; // update any conditions if you have in where clause
if(!objects.isEmpty())      
ChargentDailyUpdate.checkUpdate(objects); // static method call
   } 
}
2
  • Your Schedulable implementation doesn't ever call checkUpdate...
    – Adrian Larson
    Commented Apr 5, 2017 at 11:52
  • @AdrianLarson Thanks Larson. I have updated the code
    – sunny
    Commented Apr 5, 2017 at 16:46

You must log in to answer this question.

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