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
balraj singh 33balraj singh 33 

I am getting error while using the cron expression to run a apex class every 10 min.

String sch1 = '0 0/10 * 1/1 * ? *';
System.schedule('Scheduled Job 3', sch1, new myScheduleClass());

I generated the cron expression using tool cronmaker.

ERROR:System.StringException: Seconds and minutes must be specified as integers: 0 0/10 * 1/1 * ? *
Best Answer chosen by balraj singh 33
Ajay K DubediAjay K Dubedi
Hi Balraj,

Below code can fulfill your requirements. Hope this will work for you.

global class ScheduleEveryMinutes implements Schedulable {
   global void execute(SchedulableContext SC) {
     
      AccountCountBatch countacc = new AccountCountBatch();
      Database.executebatch(countacc);
      
     // ScheduleEveryMinutes m = new ScheduleEveryMinutes();
      //Integer sch = '0 */10 * ? * *';
     // String jobID = system.schedule('Every 10 Minutes', sch, m);
     
      // AccountCountBatch.startJob();  

        String day = string.valueOf(system.now().day());

        String month = string.valueOf(system.now().month());

        String hour = string.valueOf(system.now().hour());

        String minute = string.valueOf(system.now().minute() + 10);

        String second = string.valueOf(system.now().second());

        String year = string.valueOf(system.now().year());

        String strJobName = 'Job-' + second + '_' + minute + '_' + hour + '_' + day + '_' + month + '_' + year;

        String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;

        System.schedule(strJobName, strSchedule, new ScheduleEveryMinutes());
   
   }
}

Please mark this as best answer if this solves your problem.

Thank you
Ajay Dubedi

All Answers

Manish Chandra 5Manish Chandra 5
Hi Balraj,

You don't need a tool for creating cron expresion. Cron expression should be in following format.

Seconds Minutes Hours Day_of_month Month Day_of_week optional_year

For example if you want to schedule it for every 10 minutes then it should be '0 10 * * * *'
if you want to schedule for 12 PM every day then it should be '0 0 12 * * ?'

I hope I've answered your query

Thanks
Manish
Manish Chandra 5Manish Chandra 5
for more info ? denotes NO Value
* denotes All Value
krishna23krishna23
Hi Balraj,

Execute below code in an anonymous block. This code will execute every 10 min entire day

String sch1 = '0 0 * * * ?';
String jobId = System.schedule(' - (Every 5min)',  sch1, new myScheduleClass());


String sch3 = '0 10 * * * ?';
String jobId = System.schedule('(Every 10min)',  sch2, new myScheduleClass());


String sch3 = '0 20 * * * ?';
String jobId = System.schedule('(Every 20min)',  sch3, new myScheduleClass());


String sch4 = '0 30 * * * ?';
String jobId = System.schedule('(Every 30min)',  sch4, new new myScheduleClass());


String sch5 = '0 40 * * * ?';
String jobId = System.schedule('(Every 40min)',  sch5, new myScheduleClass());


String sch6 = '0 50 * * * ?';
String jobId = System.schedule('(Every 50min)',  sch6, new myScheduleClass());

Thanks,
Krishna
balraj singh 33balraj singh 33
so in this way i have run six different jobs. Means six jobs in the schedule queue right?
krishna23krishna23
yes Balraj every hour six jobs will run
Ajay K DubediAjay K Dubedi
Hi Balraj,

Below code can fulfill your requirements. Hope this will work for you.

global class ScheduleEveryMinutes implements Schedulable {
   global void execute(SchedulableContext SC) {
     
      AccountCountBatch countacc = new AccountCountBatch();
      Database.executebatch(countacc);
      
     // ScheduleEveryMinutes m = new ScheduleEveryMinutes();
      //Integer sch = '0 */10 * ? * *';
     // String jobID = system.schedule('Every 10 Minutes', sch, m);
     
      // AccountCountBatch.startJob();  

        String day = string.valueOf(system.now().day());

        String month = string.valueOf(system.now().month());

        String hour = string.valueOf(system.now().hour());

        String minute = string.valueOf(system.now().minute() + 10);

        String second = string.valueOf(system.now().second());

        String year = string.valueOf(system.now().year());

        String strJobName = 'Job-' + second + '_' + minute + '_' + hour + '_' + day + '_' + month + '_' + year;

        String strSchedule = '0 ' + minute + ' ' + hour + ' ' + day + ' ' + month + ' ?' + ' ' + year;

        System.schedule(strJobName, strSchedule, new ScheduleEveryMinutes());
   
   }
}

Please mark this as best answer if this solves your problem.

Thank you
Ajay Dubedi
This was selected as the best answer
dinesh Devaraj 7dinesh Devaraj 7
Hi Ajay,
Howto execute this above code.