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.

1 Answer 1

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? Sep 3, 2015 at 10:59
  • You may use this in batch class and scheduled it on daily bases or as per your requirements. Sep 3, 2015 at 11:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .