function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ms Scottms Scott 

Can't push Scheduler Class Apex into Production

Hey, I was given this code to update a date field every night and it works great in my sandbox. But the code they gave me to use for Test Class is not working and giving me errors.

I really need to get this code into production ASAP.

Scheduler Class that's working in my Sandbox;

global class SchedulerToUpdateDate implements Schedulable {
List<Grad_Employment_Detail__c> allRec = new List<Grad_Employment_Detail__c>();
List<Grad_Employment_Detail__c> toUpdate = new List<Grad_Employment_Detail__c>();
    global void  execute(SchedulableContext sc){
        allRec = [select id, current_Date__c from Grad_Employment_Detail__c];
        for(Grad_Employment_Detail__c ge: allRec){
            ge.current_Date__c = date.today();
            toUpdate.add(ge);
        }
        update toUpdate;
    }
}

This is the Test Class that was given, it's not working and giving error. (errors) is at the botom of the code listed)

@IsTest
    public class SchedulerToUpdateDate_Test{
        public static testmethod void unitTest(){            
            Test.starttest();
            Grad_Employment_Detail__c to = new Grad_Employment_Detail__c ();
            to.current_Date__c = date.today();
            insert to;
                SchedulerToUpdateDate wau = new SchedulerToUpdateDate();
                String sch = '0  00 1 3 * ?';
                system.schedule('Test', sch, wau);
            test.stoptest();
        }
    }

The error(s) I'm getting when trying to push into production. I specified this test only;
Method Name = unitTest

Error Message;
System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing: [Grad_Employment_Details__c]: [Grad_Employment_Details__c] 
Stack Trace: Class.SchedulerToUpdateDate_Test.unitTest: line 7, column 1


 
Best Answer chosen by ms Scott
rajat Maheshwari 6rajat Maheshwari 6

Hi Scott, 

Please follow this class, it will help you :)

 

@IsTest
    public class SchedulerToUpdateDate_Test{
        public static testmethod void unitTest(){   
        
        Grad_Employment_Summary__c Grad_Summary = new Grad_Employment_Summary__c();
        insert Grad_Summary;
        
        
            Test.starttest();
            Grad_Employment_Detail__c to = new Grad_Employment_Detail__c ();
           to.current_Date__c = date.today().addDays(-5);
           to.Grad_Employment_Details__c = Grad_Summary.id;
           insert to;
                SchedulerToUpdateDate wau = new SchedulerToUpdateDate();
                String sch = '0  00 1 3 * ?';
                system.schedule('Test', sch, wau);
            test.stoptest();
        }
    }

All Answers

rajat Maheshwari 6rajat Maheshwari 6

Hi Scott,

Please do below two approach and let me know the results : - 

1. 

@IsTest
    public class SchedulerToUpdateDate_Test{
        public static testmethod void unitTest(){            
            Test.starttest();
                SchedulerToUpdateDate wau = new SchedulerToUpdateDate();
                String sch = '0  00 1 3 * ?';
                system.schedule('Test', sch, wau);
            test.stoptest();
        }
    }


2. You need to include required field from sObject - "Grad_Employment_Detail__c"

Grad_Employment_Detail__c to = new Grad_Employment_Detail__c ();
            to.current_Date__c = date.today();
             to.RequiredFieldName = blablabla;

Thanks
Rajat Maheshwari
rajatzmaheshwari@gmail.com

rajat Maheshwari 6rajat Maheshwari 6

Hi Scott, 

Please follow this class, it will help you :)

 

@IsTest
    public class SchedulerToUpdateDate_Test{
        public static testmethod void unitTest(){   
        
        Grad_Employment_Summary__c Grad_Summary = new Grad_Employment_Summary__c();
        insert Grad_Summary;
        
        
            Test.starttest();
            Grad_Employment_Detail__c to = new Grad_Employment_Detail__c ();
           to.current_Date__c = date.today().addDays(-5);
           to.Grad_Employment_Details__c = Grad_Summary.id;
           insert to;
                SchedulerToUpdateDate wau = new SchedulerToUpdateDate();
                String sch = '0  00 1 3 * ?';
                system.schedule('Test', sch, wau);
            test.stoptest();
        }
    }
This was selected as the best answer