1

I have to create scheduled job, that will be executed every minute. Using http://advancedapex.com/2012/11/14/scheduledapex/ following code was written:

global class ScheduledOperatorStatusMonitor implements Schedulable{

    public Interface IScheduleOperatorStatusMonitor{
        void execute(SchedulableContext SC);
    } 

    global void execute(SchedulableContext SC){
        Type targettype = Type.forName('ScheduleOperatorStatusMonitorHandler');   
        if(targettype!=null) {
            IScheduleOperatorStatusMonitor obj = (IScheduleOperatorStatusMonitor)targettype.NewInstance();
            obj.execute(sc);  
            ScheduledOperatorStatusMonitor.start();
        } 
    }

  private static void start(){
        Datetime sysTime=System.now().addSeconds(60);
        String chronExpression = '' + sysTime.second() + ' ' + sysTime.minute() + ' ' + sysTime.hour() + ' ' + sysTime.day() + ' ' + sysTime.month() + ' ? ' + sysTime.year();
        System.schedule( 'ScheduledOperatorStatusMonitor ' + sysTime, chronExpression, new ScheduledOperatorStatusMonitor() );

    }    
}


public class ScheduleOperatorStatusMonitorHandler implements ScheduledOperatorStatusMonitor.IScheduleOperatorStatusMonitor {

    private  void doWork(){
          //some code here
       }        

    public void execute( SchedulableContext SC )
        {
            doWork();
        }
}

Everything is fine, only one issue - number of records for ScheduledOperatorStatusMonitor in Setup>Jobs>Scheduled Jobs is constantly increased. How code can be changed ( maybe add System.abortJob) to prevent this issue?.. It will be nice, if only one record for currently executed instance will be shown on UI.

0

You can use CronTrigger Class for this as shown below

 for(CronTrigger cron : [select Id, CronExpression,CronJobDetail.name
 from CronTrigger limit 1])
 {
     System.abortJob(cron.Id); 
 }

You can replace the above query with you needed one.

2
  • thx. where i can use this code? – anastsiacrs Sep 3 '15 at 10:59
  • You may use this in batch class and scheduled it on daily bases or as per your requirements. – Ritesh Gupta Sep 3 '15 at 11:03

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.