0

I have a batch that does three callout and then update some records, if occurs any error in the update or in the callout, a new record in the log object with the IDs of the records that occurred error is created and I also have two constructors in my batch so I can send this ID's. So, in my scheduled I wanna to run two times with different minutes, like: 4AM and 4:10AM so it will be reprocess. It's make sense? How scheduled two times a day?

global class AccessScheduler implements Schedulable {
    global void execute(SchedulableContext sc) {
        
        List<Log__c> log = [SELECT SentData__c FROM Log__c WHERE CreatedDate=: System.today()];
        
        if(log.size() == 0) {
            AccessBatch batch = new AccessBatch(); 
            Database.executeBatch(batch, 50);
        } 
        else {
            for(Log__c logItem : log){
                String values = logItem.SentData__c;
                List<String> userValues = values.split(', ');
    
                AccessBatch batch = new AccessBatch(userValues); 
                Database.executeBatch(batch, 50);
            }
        }
    }
}
3
  • 3
    Just schedule two same jobs, one at 4AM second at 4:10 AM each day Commented Oct 29, 2022 at 20:21
  • And how to test it? I tried to call the first schedule forcing an error and creating the log object and then call again the schedule but don't work
    – Alexia
    Commented Oct 29, 2022 at 20:25
  • 1
    Why do you need to test it? Do you doubt that Salesforce will invoke the job appropriately? If you mean how to test execution with and without Log__c records, that is easy. When you write your tests, do not actually try to schedule the execution, just have two separate tests that instantiate it (with or without logs) and explicitly call the execute method with null or a fabricated context if you need one. If you use Test.startTest and Test.stopTest appropriately a single execution of pending async processes will be run when the stop is called.
    – Phil W
    Commented Oct 29, 2022 at 21:04

1 Answer 1

0

To schedule two jobs, use two scheduling statements:

System.schedule('4AM RUN', '0 0 4 ? * *', new AccessScheduler());
System.schedule('4AM RUN', '0 10 4 ? * *', new AccessScheduler());

To test this, you need two unit tests:

@isTest static void test1() {
  Test.startTest();
  System.schedule('4AM RUN TEST', '0 0 4 ? * *', new AccessScheduler());
  Test.stopTest();
  // Do assertions here
}

@isTest static void test2() {
  Log__c testLog = new Log__c(SentData='whatever');
  insert testLog;
  Test.startTest();
  System.schedule('4AM RUN TEST', '0 0 4 ? * *', new AccessScheduler());
  Test.stopTest();
  // Do assertions here
}

When writing unit tests, you'll find you often need to set up various test data before starting the test. You can't do "end to end" testing all the time, which is what it sounds like you tried to do. Each test should add to the code coverage so that, when done, everything is covered.

You must log in to answer this question.

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