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
Kourt NeyraKourt Neyra 

Scheduled class is not running daily

Hi
I have a scheduled class in my org that is used to delete records.
We have a field "Policy End Date" and daily, we need to delete the records where Policy end date is less than today.
The issue is that it is working fine the first day that we schedule it, but the following days it is not deleting the records.
someone know how to fix it?
global class BorrarAsegurados implements Schedulable{
    global void execute(schedulablecontext SC){
        List<Enrollee__c> Asegurados = [select id from Enrollee__c where Policy_End_Date__c <= Yesterday ];
        if(!Asegurados.isEmpty())
            delete Asegurados;
    }

}

 
Balayesu ChilakalapudiBalayesu Chilakalapudi
Call your class from another class like this,
BorrarAsegurados m = new BorrarAsegurados();
//run at every midnight
String sch = '0 0 0 * * ?';
String jobID = system.schedule('BorrarAsegurados Job', sch, m);
Kourt NeyraKourt Neyra
Oh ok I understand, then when I use the apex schedule UI, the class that I need to schedule is this second class that you mention? 
it would be something like: 
global class secondclass implements Schedulable{
    global void execute(schedulablecontext SC){
       BorrarAsegurados m = new BorrarAsegurados();
       //run at every midnight String sch = '0 0 0 * * ?';
       String jobID = system.schedule('BorrarAsegurados Job', sch, m);
  }
}
it would be something like that? 

Thank you very much fro your help
 
Balayesu ChilakalapudiBalayesu Chilakalapudi
If you are using UI, second class is not required, just set the frequency properly in UI and follow the steps like this,

From Setup, enter Apex in the Quick Find box, then select Apex Classes.
Click Schedule Apex.
For the job name, enter something like Daily Oppty Reminder.
Click the lookup button next to Apex class and enter * for the search term to get a list of all classes that can be scheduled. In the search results, click the name of your scheduled class.
Select Weekly or Monthly for the frequency and set the frequency desired.
Select the start and end dates, and a preferred start time.

Click Save.

for testing your scheduler refere below link
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm
Kourt NeyraKourt Neyra
sorry, I have already scheduled the class using the UI, and it works perfectly the first run, but the second day is when the class is not deleting any records. I was wondering if it could be something wrong when using the date literals in a scheduled apex class or somethig that I am missing
Krishnamoorthi PeriasamyKrishnamoorthi Periasamy
Kourt,  Can you provide screenshot of UI configuration? Did you check everyday job is running? If yes, are you sure you have data to delete after 1st run?

Thanks
Krishna
Kourt NeyraKourt Neyra
User-added image
hi, this is the configuration, and there are 5300 records that should have been deleted today. for tomorrow, there are 23,000

Thanks in advance.
Krishnamoorthi PeriasamyKrishnamoorthi Periasamy
Did you see in job log whether job ran for today or not? i see job start date is today only...
Kourt NeyraKourt Neyra
I aborted the previous job that I scheduled on wendnesday because it ran 30/03/2017 at 02:00, but it didn't run on 31/03/2017. This screenshot is a new scheduled job I created today so the class runs tonight (01/04/2017 at 02:00 AM).
Krishnamoorthi PeriasamyKrishnamoorthi Periasamy
So your job did not run at all on 31/03/2017. If you had selected to run job on all days, it should have started and created job log info. It is really strange why it is not started when you scheduled for every day....could you please check 'Scheduled Jobs' log info for 31/03/2017?