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
Andreea TAndreea T 

How to run a shedulable class every 1 min

How to run a schedulable class every 1 min?
Best Answer chosen by Andreea T
NagendraNagendra (Salesforce Developers) 
Hi Jangam,

Please find the sample code below.
global class TestSchedulableClass implements Schedulable 
{
    global void execute(SchedulableContext ctx) 
	{           
		// add your logic here	
      
        //schedule next execution after a minute
        DateTime currentDateTime=System.now().addMinutes(1);                            
        String nextScheduleTime=String.valueof(currentDateTime.second()) +' '+String.valueof(currentDateTime.minute())+' '
                                +String.valueof(currentDateTime.hour())+' '+String.valueof(currentDateTime.day())+' '
                                +String.valueof(currentDateTime.month())+' ? '+String.valueof(currentDateTime.Year());
        
        TestSchedulableClass testObj = new TestSchedulableClass();
		
        system.schedule('Scheduled at '+System.now().format(), nextScheduleTime, testObj);
        
        for( CronTrigger cronTriggerItem:[Select Id From CronTrigger where  
                                    NextFireTime= null  AND State='DELETED' Limit 100])
		{
                System.abortJob(cronTriggerItem.id);
        }       
   }   
}
Please try below code to execute.
String chron1 = '0 1 * * * ? *';      
System.schedule('Every 1 min', chron1, new TestSchedulableClass());
Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra

 

All Answers

NagendraNagendra (Salesforce Developers) 
Hi Jangam,

Please find the sample code below.
global class TestSchedulableClass implements Schedulable 
{
    global void execute(SchedulableContext ctx) 
	{           
		// add your logic here	
      
        //schedule next execution after a minute
        DateTime currentDateTime=System.now().addMinutes(1);                            
        String nextScheduleTime=String.valueof(currentDateTime.second()) +' '+String.valueof(currentDateTime.minute())+' '
                                +String.valueof(currentDateTime.hour())+' '+String.valueof(currentDateTime.day())+' '
                                +String.valueof(currentDateTime.month())+' ? '+String.valueof(currentDateTime.Year());
        
        TestSchedulableClass testObj = new TestSchedulableClass();
		
        system.schedule('Scheduled at '+System.now().format(), nextScheduleTime, testObj);
        
        for( CronTrigger cronTriggerItem:[Select Id From CronTrigger where  
                                    NextFireTime= null  AND State='DELETED' Limit 100])
		{
                System.abortJob(cronTriggerItem.id);
        }       
   }   
}
Please try below code to execute.
String chron1 = '0 1 * * * ? *';      
System.schedule('Every 1 min', chron1, new TestSchedulableClass());
Hope this helps.

Kindly mark this as solved if the reply was helpful.

Thanks,
Nagendra

 
This was selected as the best answer
Ajay K DubediAjay K Dubedi
Hi Imo,
You can schedule the job in every two minutes through the following code:
String scheduleTime = '0 0/2 * 1/1 * ? *';
BatchClassName batchInstance = new BatchClassName();
system.schedule('Schedule Every 2 min', scheduleTime, batchInstance);
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Deepali KulshresthaDeepali Kulshrestha
Hi Imo Jangam,

You have to schedule your batch for 60 times if you want to run a batch for every 1 minute.
Like this:-

for(Integer i = 0 ; i < 60 ; i++){
    System.schedule('Scheduled Job '+i , '0'+i+' * * * ?', new scheduledTest());
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha
Andreea TAndreea T
And what about the governor limits? It won't affect?
Andreea TAndreea T
@Nagendra, this batch is running every half an hour. 
@Ajay K Dubedi this is giving me error: System.StringException: Seconds and minutes must be specified as integers: 0 0/2 * 1/1 * ? *
Ajay K DubediAjay K Dubedi
Hi Imo,
Try the following code, it works for me:
Public static void SchedulerMethod() {  
     String scheduleTime = '0 0 * * * ?';
     BatchClassName batchInstance = new BatchClassName();
    system.schedule('Schedule Every Minute', scheduleTime,batchInstance);
   }

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi