I have a class that is scheduled using CRON. I have created a test class and the test for the CRON is tested ok. However, the field update is not being tested and I cannot see why. Coverage is 57% with 4/6 pass. Could anyone help? thanks


global class ScheduleEmailInvoices implements Schedulable {

global void execute(SchedulableContext sc) {

List<Email_Notifications__c > acclist = [Select SendStatus__c,QualifyDate__c from Email_Notifications__c where SendStatus__c = 'Completed' and QualifyDate__c =Today];

        for(Email_Notifications__c a: acclist)


               a.SendStatus__c = 'Ready to send';

        update acclist;


Test Class.

private class ScheduleEmailInvoicesTest {

    static testMethod void myTestMethod() {


    ScheduleEmailInvoices myClass = new ScheduleEmailInvoices ();   
    String chron = '0 00 07 * * ?';        
    system.schedule('Schedule Email InvoicesTest', chron, myClass);
    system.assert([SELECT COUNT() FROM CronTrigger] > 0);



 static testMethod void validateStatus() {
 Email_Notifications__c b = new Email_Notifications__c(Name='test', SendStatus__c ='Completed',QualifyDate__c = system.today());

 // Insert record
 insert b;


 b = [SELECT SendStatus__c FROM Email_Notifications__c WHERE Id =:b.Id];

 System.assertEquals('Ready to send', b.SendStatus__c);

  • What specific problem you are facing here? – rahul gawale May 29 at 12:18
  • the update to status is being highlighted as a fail in the test; – user2382031 May 29 at 12:21

You need to do this in a single test. Data processed by one test method is not available to another. Each method has its separate transaction.

  1. Insert the test records.
  2. Schedule the job, after the startest().
  3. Query the result and assert after the stopTest().
| improve this answer | |
  • ok. I will try that. thanks Rahul – user2382031 May 29 at 12:45
  • 1
    perfect. Thanks again Rahul – user2382031 May 29 at 12:49
  • @user2382031 please mark as accepted if it works for you. – rahul gawale May 29 at 12:51
  • ok. just accepted. can I score your response. Sorry not sure how – user2382031 May 29 at 13:09
  • 1
    ok thanks again – user2382031 May 29 at 13:14

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.