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
Bhaskar AroraBhaskar Arora 

Schedule a job in every 5 Minutes.

If you want to run a scheduler every 5 Minutes.

 

Note:Every thing has to be done in scheduler class.

 

1.you have to run a scheduler for the first time so do it on action of click of a button or from Developer console
say for example at 10 am.

 

2.In scheduler class use gettrigger id.

 

3.When scheduled job runs you will get the scheduler job id in scheduler class.

 

4.so write a query on Cron trigger where you will get cron Expression related to the scheduled job id when class is called.

 

5.extract the mintes from the cron expression using string functions.

 

6.now schedule a job with current hour and adding five minutes in cron expression minutes u get.

 

7.
if(Minutes==55 )
{
minute=0;

}
else
{
minute=minute+5;
}


Code is like this...

string cronid;
cronid=sc.getTriggerId();

CronTrigger scheduledjob=new CronTrigger(Id=cronid);
string value=[Select CronExpression From CronTrigger where id=:cronid limit 1].CronExpression;
List <string> value1 =value.split(' ');

system.abortJob(scheduledjob.Id); //abort scheduled job

Datetime sysTime = System.now();
integer minute=integer.valueof(value1[1]);
integer hour1=sysTime.hour();
if(integer.valueOf(value1[1])==55)
{
minute=0;
}
else
{
minute=minute+5;
}

String chron_exp = '' + sysTime.second() + ' ' + minute + ' 0-23 * * ?';
SchedulerMassEmail scheduler = new SchedulerMassEmail();
string id=System.schedule('MassEmail Scheduler',chron_exp,scheduler);

 

So the schedule job will be made with the same name every time.

 

There is one more way of doing it.

schedule job say for

 

 string sch22='0 0 * * * ?';     

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

string sch22='0 10 * * * ?';

string sch22='0 15 * * * ?';

string sch22='0 20 * * * ?';

string sch22='0 25 * * * ?';

string sch22='0 30 * * * ?';

string sch22='0 35 * * * ?';

string sch22='0 40 * * * ?';

string sch22='0 45 * * * ?';

string sch22='0 50 * * * ?';

string sch22='0 55 * * * ?';

so sy these 12 jobs will run once in an hour,

 

so sy at 10 then 10.05 and the 10.10 like this . :):)

Avidev9Avidev9

I guess you can use the new System.scheduleBatch();

 

Something like this

[The below example will create a chainning effect and it will schedule itself after every 5min from the finsih method]

 

global class MyBatch implements Database.Batchable<sObject>{
 
   global MyBatch(){
        
   }

   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator('Your Query');
   }

   global void execute(Database.BatchableContext BC, 
                       List<Sobject> scope){
} global void finish(Database.BatchableContext BC){ System.scheduleBatch(new MyBatch(), 'job example', 5);
//here 5 is the time interval
} }
Bhaskar AroraBhaskar Arora

Actually the scenario i supposed is::

 

i wanted that the batch will be called at  exact time i.e  10 am,10:5 am ,10:10 am and so on

 

so schedule job will run exact time  whether batch is finished or not.

 

 

Avidev9Avidev9
So instead of calling from finish method you can call the scheduleBatch from the startMethod.
Bhaskar AroraBhaskar Arora

According to my logic , we need to execute batch only when their is some record generated ,

So the schedule job will be created and abort even if there are no records ,so that the continuity of scheduling job will not break.

 

So using a single job we can run it recursively every five minutes.

bob_buzzardbob_buzzard

Just to be a but pedantic, this won't run a scheduled job every 5 minutes, it will schedule a job to run five minutes after the last one.  Even if Force.com wasn't a multi-tenant platform you would get a short drift every time, due to the number of CPU cycles required to fire up the job and start executing it.  In this case, as Force.com makes no guarantees about when scheduled jobs run, I'd expect to see a noticeable drift within a couple of days.

Bhaskar AroraBhaskar Arora

Hi,

 

What you said is correct the  lack of resources can cause this Logic to fail,but i am running it from a month its working fine.For precaution, i will try to find any other solution to do my required job.

 

Thanks