TransWikia.com

Scheduled apex class not updating records and test not covering

Salesforce Asked by J.0 on January 29, 2021

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();
    }
}

One Answer

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);
        
    }
}

Answered by Barış Arslan on January 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP