+ Start a Discussion
Faizan Ali 24Faizan Ali 24 

Scheduled class isn't running

Hi all,

I am trying to automate the creation of records every 6 months however it is not working and I don't know how to debug it.

My scheduled class:
global class ScheduleRosterAutomation implements Schedulable{
    
    global void execute(SchedulableContext SC) {
        AutomateRosterCreationHandler.createRosters();
        
     }
}



I have a class that creates rosters every 6 months:
public with sharing class AutomateRosterCreationHandler{

    public static List<Employee_Roster__c> createRosters()
    {
        // Get all contacts with standard hours.
        List<Contact> contacts = getContactsWithStdHrs();
        // Create an empty list of Employee Roster objects.
        // This will be used to insert the records into the database.
        List<Employee_Roster__c> empRostList = new List<Employee_Roster__c>();
        // Start Date for each week.
        // uses setMondayStartDate to change the date each week.
        Date startDate = Date.Today().addDays(1);
        //For 26 weeks out of 52 we want to create a roster for each contact each week.
        for (Integer i =0; i < 26; i++)
        {
            // For each contact fetch the standard hours sObject and field values.
            // Assign the values to be input for the Employee Roster.
            // Note: The field used in the UI is Time and the Employee Roster fields are Date/Time.
            // Values will need to be formatted and concatenated for Roster creation.
            for (Contact con : contacts)
            {
                // Get the Standard Hours from the contact
                // Assign the values from the fields to local variables
                Standard_Hours__c standardHours = getStandardHours(con);
                Time monStart;
                Time monEnd;
                Time tueStart;
                Time tueEnd;
                Time wedStart;
                Time wedEnd;
                Time thuStart;
                Time thuEnd;
                Time friStart = standardHours.Friday_Start__c;
                Time friEnd = standardHours.Friday_End__c;
                Time satStart = standardHours.Saturday_Start__c;
                Time satEnd = standardHours.Saturday_End__c;
                Time sunStart = standardHours.Sunday_Start__c;
                Time sunEnd = standardHours.Sunday_End__c;
                
                //Check if each weekday has a start and end time value
                //if it has no value place an empty Time value in.
                if (standardHours.Monday_Start__c != null)
                {
                    monStart = standardHours.Monday_Start__c;
                }else{
                    monStart = Time.newInstance(0, 0, 0, 0);
                }
                
                if (standardHours.Monday_End__c != null)
                {
                    monEnd = standardHours.Monday_End__c;
                }else{
                    monEnd = Time.newInstance(0, 0, 0, 0);
                }
                
                if (standardHours.Tuesday_Start__c != null)
                {
                    tueStart = standardHours.Tuesday_Start__c;
                }else{
                    tueStart = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Tuesday_End__c != null)
                {
                    tueEnd = standardHours.Tuesday_End__c;
                }else{
                    tueEnd = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Wednesday_Start__c != null)
                {
                    wedStart = standardHours.Wednesday_Start__c;
                }else{
                    wedStart = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Wednesday_End__c != null)
                {
                    wedEnd = standardHours.Wednesday_End__c;
                }else{
                    wedEnd = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Thursday_Start__c != null)
                {
                    thuStart = standardHours.Thursday_Start__c;
                }else{
                    thuStart = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Thursday_End__c != null)
                {
                    thuEnd = standardHours.Thursday_End__c;
                }else{
                    thuEnd = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Friday_Start__c != null)
                {
                    friStart = standardHours.Friday_Start__c;
                }else{
                    friStart = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Friday_End__c != null)
                {
                    friEnd = standardHours.Friday_End__c;
                }else{
                    friEnd = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Saturday_Start__c != null)
                {
                    satStart = standardHours.Saturday_Start__c;
                }else{
                    satStart = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Saturday_End__c != null)
                {
                    satEnd = standardHours.Saturday_End__c;
                }else{
                    satEnd = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Sunday_Start__c != null)
                {
                    sunStart = standardHours.Sunday_Start__c;
                }else{
                    sunStart = Time.newInstance(0,0,0,0);
                }
                
                if (standardHours.Sunday_End__c != null)
                {
                    sunEnd = standardHours.Sunday_End__c;
                }else{
                    sunEnd = Time.newInstance(0,0,0,0);
                }
                
                Contact cont = con;
                // Create new empty Employee Roster
                Employee_Roster__c empRost = new Employee_Roster__c();
                
                
                // Assign the values for the Employee Roster
                // Format the date/time fields accordingly using the values from the local variables.
                
                emprost.Week_Beginning__c = startDate;
                emprost.Contact__c = con.Id;
                emprost.Monday_Start__c = DateTime.newInstance(startDate, monStart);        
                emprost.Monday_End__c = DateTime.newInstance(startDate, monEnd);
                emprost.Tuesday_Start__c = DateTime.newInstance(startDate.addDays(1), tueStart);
                emprost.Tuesday_End__c = DateTime.newInstance(startDate.addDays(1), tueEnd);
                emprost.Wednesday_Start__c = DateTime.newInstance(startDate.addDays(2), wedStart);
                emprost.Wednesday_End__c = DateTime.newInstance(startDate.addDays(2), wedEnd);
                emprost.Thursday_Start__c = DateTime.newInstance(startDate.addDays(3), ThuStart);
                emprost.Thursday_End__c = DateTime.newInstance(startDate.addDays(3), thuEnd);
                emprost.Friday_Start__c = DateTime.newInstance(startDate.addDays(4), friStart);
                emprost.Friday_End__c = DateTime.newInstance(startDate.addDays(4), friEnd);
                emprost.Saturday_Start__c = DateTime.newInstance(startDate.addDays(6), satStart);
                emprost.Saturday_End__c = DateTime.newInstance(startDate.addDays(6), satEnd);
                emprost.Sunday_Start__c = DateTime.newInstance(startDate.addDays(7), sunStart);
                emprost.Sunday_End__c = DateTime.newInstance(startDate.addDays(7), sunEnd);
                
                // add the roster to the list of rosters
                empRostList.add(emprost);
            }
            startDate = setMondayStartDate(startDate);
        }
        
        // Persist the list of rosters.
        insert empRostList;
        return empRostList;
    }
    
    // Add 7 days to the last start date.
    public static Date setMondayStartDate(Date lastStartDate)
    {
        Date newDate = lastStartDate.addDays(7);
        return newDate;
    }

    // Get a list of all contacts with Standard Hours
    public static List<Contact> getContactsWithStdHrs()
    {
        List<Contact> conts = [SELECT Id, Name, Has_Standard_Hours__c FROM Contact WHERE RecordType.Name IN ('Team Contact') AND Contact.Has_Standard_Hours__c = true];
        return conts;
    }

    // Get standard hours from a specific contact.
    public static Standard_Hours__c getStandardHours(Contact con)
    {
        Standard_Hours__c  stdHrs;
        // Check if contact has standard hours
        if (con.Has_Standard_Hours__c == True)
        {
            // populate a Standard Hours sObject with values based on the contact
            stdHrs = [SELECT Id, Name, Monday_Start__c, Monday_End__c, Tuesday_Start__c, Tuesday_End__c, Wednesday_Start__c, Wednesday_End__c, Thursday_Start__c, Thursday_End__c, Friday_Start__c, Friday_End__c, Saturday_Start__c, Saturday_End__c, Sunday_Start__c, Sunday_End__c, Contact__c FROM Standard_Hours__c where Contact__c =: con.Id LIMIT 1];
            return stdHrs;
        }
        else{
            return stdHrs;
        }
    }
    
    public static void AutomateRosterCreationHandler()
    {
        createRosters();
    }
}

 
AnudeepAnudeep (Salesforce Developers) 
Hi Faizan, 

I recommend checking if the SOQL queries are returning any results in the first place. Add system debug statements to methods getContactsWithStdHrs and getStandardHours and schedule the apex through developer console/execute anonymous window. Please see 
Debugging Schedulable Job in Apex Salesforce to learn more

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you