Skip to main content Join us at TDX in San Francisco or on Salesforce+ on March 5-6 for the Developer Conference for the AI Agent Era. Register now.
I am trying to Schedule an class at the interval of 5 minutes but am not getting any idea how to perform it, i have seen some examples but they are not working properly please help if any body can.
8 answers
  1. Jun 27, 2016, 7:16 AM

    Say, you want the class to run every 1 hour, or every 30 mins, then you cannot do it using declarative way. To do this, we can use system.schedule method and call the scheduler class. System.schedule takes 3 parameters as below :

    system.schedule('jobname',timeperiod,scheduler class name)

    jobname: you can give any name to your job

    timeperiod: specify the time frequency with which the job should run

    scheduler class name: scheduler class that calls your batch apex class.

     timeperiod should be specified in below format:

    Seconds Minutes Hours Day_of_month Month Day_of_week optional_year

    For example, to run every 5 mins the expression would be : 0 5 * * * ?

    here '*' means every time against the specified parameter, every hour, every day of month, every day of week and '?' means no specific value. Thus, this expression means every 5 minutes. So, the system.schedule('testname',0 35 * * * ?,schedulerclass()); would call the schedulerclass every 30 minutes when the statement executes.You can visit salesforce documentation to know more on system.schedule parameters.

    Lets write a simple batch apex class, its scheduler class and then, execute system.schedule method to run that batch apex every 5 minutes.

    Batch Apex Class:

    global class BatchApexDemo implements database.batchable<sobject>{

    Public string soqlquery;

    Public void setQry(string soqlquery){

    this.soqlquery = 'Select name,status from account limit 1';

    }

    global database.querylocator start(database.batchableContext bc){

    return database.getquerylocator(soqlquery);

    }

    global void execute(database.batchablecontext bd, list<sobject> sc){

    System.debug('**In Execute Method**');

    }

    Public void finish(database.batchableContext bc){

    }

    }

    Scheduler class along with method that will Can scheduler class every 5 minutes:

     

    global with sharing class ScheduleBatchApexDemo implements Schedulable {

    global void execute(SchedulableContext sc) {

    ID BatchId = Database.executeBatch(new BatchApexDemo(), 200);

    }

    Public static void SchedulerMethod() {

    string timeinterval = '0 5 * * * ?';

    System.schedule('BatchApexDemo-Every15mins',timeinterval, new ScheduleBatchApexDemo());

    }

    }

    Now, we have to just execute the "SchedulerMethod" once, this will keep on calling the batch apex every 5 minutes. We can use developer console to execute this method. Just execute below statement to call the method: ScheduleBatchApexDemo.SchedulerMethod(); You can monitor the batch apex being called every 5 minutes from:

    set up--> monitoring jobs

    Please mark this as the best answer if this helps

     
  2. Jan 13, 2020, 9:01 PM
    Vasani's answers above could not be more wrong! 

    '0 5 * * * ?' means the 5th minute of every hour, NOT every five minutes!

     
  3. Jun 27, 2016, 7:05 AM
    Abhi - Welcome to this vibrant community

    This problem can easily be solved using apex:

    System.schedule('Scheduled Job 1', '0 5 * * * ?', new ScheduledClass());

    Please mark this as the best answer if this helps
  4. Jun 27, 2016, 7:04 AM

    try this

    System.schedule('Jobname', '0 5 * * * ?', new scheduleclass());

  5. Jun 27, 2016, 7:15 AM
    You can simply paste this above code in developer console anonymous window by replacing job name with your exact job name and schedule class with your schedule class and click execute.

    after execute you can check your scheduler in Scheduled Jobs under setup.
0/9000