1

I have been working on a scheduled apex class to update cases when their status contains future and when the my field (date/time)future date of completion = today. My problem is that its running and the SOQL is gathering the list however none of the cases that meet the criteria are being updated. Any suggestions would be greatly appreciated.

Apex Scheduled class

public class FutureDatedCasesScheduler implements Schedulable {
    public void execute(SchedulableContext sc) {
        List<Case> FutureCases = ([Select Id, Status, Future_Date_for_Completion__c 
                                   from Case Where Status <> 'Future' AND Future_Date_for_Completion__c !=Null]);
        For (Case CaseList: FutureCases){
            if (FutureCases[0].Future_Date_for_Completion__c == Date.today()){ 
                FutureCases[0].Status = 'New';
                Update FutureCases;
            }
        }      
    }
}

Test Class

@istest
public class FutureDatedCasesTest {
    public static void  FutureDatedCasesScheduler() {
        Test.StartTest();
        FutureDatedCasesScheduler sh1 = new FutureDatedCasesScheduler();      
        String sch = '0 0 23 * * ?';
        system.schedule('Test check', sch, sh1);
        Test.stopTest();
    }
}
  • 1
    Hi, not sure if it's a mistake but you wrote "... update cases when their status contains future..." but the SOQL condition is "Where Status <> 'Future'" – Json Aug 5 at 13:41
  • In unit tests, you are responsible for creating the data for the test. If you don't create any Cases in your test, prior to when your code is actually executed (so, before Test.stopTest() in this instance), then your query will return no results and you won't get coverage for the code inside the for loop. Also your test is not performing any assertions (arguably the most important part of a unit test). There's more advice in salesforce.stackexchange.com/questions/244794/… – Derek F Aug 5 at 13:54
0

I will suggest few things.

1-) Do not use a dml statement (like update,insert,delete) in loops

2-) You can change query to get only the records you need to update

3-) For test class create some records and run the scheduled job (you should also add some negative test cases and write some assertions)

Apex Scheduled class

public class FutureDatedCasesScheduler implements Schedulable {
    public void execute(SchedulableContext sc) {
        List<Case> futureCases = [SELECT Id, Status, 
                                  Future_Date_for_Completion__c 
                                  FROM Case 
                                  WHERE Status = 'Future' 
                                  AND Future_Date_for_Completion__c = TODAY ];
        for(Case cs: futureCases){
            cs.Status = 'New';
        }   
        update futureCases;     
    }
}

Test Class

@istest
public class FutureDatedCasesTest {
    @isTest static void  futureDatedCasesScheduler() {
    
        //create case
        Case newCase = new Case(
            Subject='Test',
            Status='Future',
            Origin='Web',
            Future_Date_for_Completion__c=Date.today());
        
        insert newCase;
        
        Test.StartTest();
        FutureDatedCasesScheduler sh1 = new FutureDatedCasesScheduler();      
        String sch = '0 0 23 * * ?';
        system.schedule('Test check', sch, sh1);
        Test.stopTest();
        
        //an example assertion
        Case testCase = [SELECT Id,Status FROM Case WHERE Subject='Test' LIMIT 1];
        System.assertEquals('New',testCase.Status);
        
    }
}
| improve this answer | |

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.