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
venkyyyvenkyyy 

using batch class i want to schedule a batch class to print hello wold for every minute.. even if user get logged out.

Hi all, 
I have a scenario that i want to schedule a batch class for every minute even if user get logged out.
for that i just created one basic batch class which i want to get in debug (please have a look bellow code).

if i run this throug execute ananomus window, it throws an error is: Invalid token "helloworld".

give me the lines what r the modifications i need to do.

batch class is1:
--------------------------
global class SampleBatchClass implements Database.Batchable<sObject>{

      // Start Method
        global Database.QueryLocator start(Database.BatchableContext BC){
        string query = 'hellow world';
         return Database.getQueryLocator(query);
        }
      
      // Execute Logic
       global void execute(Database.BatchableContext BC, List<sObject>scope){

             system.debug('>>>>>inside batcg<<<<<<<<<<'+scope);   
     
       }
     
       global void finish(Database.BatchableContext BC){
            // Logic to be Executed at finish
       }
    }

scheduled class is :
--------------------------------
global class sampleschdeulerclass implements schedulable
{
    global void execute(SchedulableContext sc)
    {
    SampleBatchClass sbc = new SampleBatchClass(); 
      database.executebatch(sbc);
      system.debug('>>>>>>>>inside schedule class<<<<<<<<<');
    }
}
Fahad-AkhtarFahad-Akhtar
HI Venkyyy,
Please see my comments below and do share the problem you are trying to solve.
global class SampleBatchClass implements Database.Batchable<sObject>{

      // Start Method
        global Database.QueryLocator start(Database.BatchableContext BC){
        //You cant put hello world in your query, it should be a query string.
         string query = 'hellow world';
         return Database.getQueryLocator(query);
        }
      
      // Execute Logic
       global void execute(Database.BatchableContext BC, List<sObject>scope){
             //You can check your hello world in debug log, loop through your list of Sobject.
             system.debug('>>>>>inside batcg<<<<<<<<<<'+scope);   
     
       }
     
       global void finish(Database.BatchableContext BC){
            // Logic to be Executed at finish
       }
    }

scheduled class is :
--------------------------------
global class sampleschdeulerclass implements schedulable
{
    global void execute(SchedulableContext sc)
    {
    SampleBatchClass sbc = new SampleBatchClass(); 
      database.executebatch(sbc);
      system.debug('>>>>>>>>inside schedule class<<<<<<<<<');
    }
}

Following are the issues:
- You are using query variable to save "Hello World" and its not correct query.
- If you need to run your batch every minute you will potentially run out of concurrent batch very quickly, I would recommend you to use finish method of batch to invoke it self again, this way you can chain your batches and will not run out of limit if one of the batch takes longer in processing.


Thanks,
Fahad Akhtar
Amit Chaudhary 8Amit Chaudhary 8
Hi,

Batch job is not required here. You can directly created on scheduler class. Please try below class. I hope that will help you
global class SampleSchedulableClass implements Schedulable 
{ 
    global void execute(SchedulableContext ctx) 
	{           
		system.debug('>>>>>Hellow world<<<<<<<<<<'); 		

		
        //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());

								
		//schedule next execution after a minute
		SampleSchedulableClass obj = new SampleSchedulableClass ();
        system.schedule('Scheduled at '+System.now().format(), nextScheduleTime, obj);
        
        //delete completed apex scheduled jobs for which state is DELETED to keep the Scheduled APex log cleaner        
        for( CronTrigger cronTriggerItem:[Select Id From CronTrigger where  
                                    NextFireTime= null  AND State='DELETED' Limit 100]){
                System.abortJob(cronTriggerItem.id);
        }       
   }   
}

Please let us know if this will help you

Thanks,
Amit Chaudhary
venkyyyvenkyyy
Thank you amith,
I tried the code what you gave, it is running successfully but i get only once the  '>>>>>Hellow world<<<<<<<<<<' in debug log and i am confusing to see how it'll works with every minute,? how can i see it.   Can you guide me with the steps plz.
Its needfull to me.

Thanks in advance again.  :)
Amit Chaudhary 8Amit Chaudhary 8
You can check the schedule job from monitering or you can check sechedule job status


Below code will schedule your class again and again:-

  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());

                                
        //schedule next execution after a minute
        SampleSchedulableClass obj = new SampleSchedulableClass ();
        system.schedule('Scheduled at '+System.now().format(), nextScheduleTime, obj);
        
        //delete completed apex scheduled jobs for which state is DELETED to keep the Scheduled APex log cleaner        
        for( CronTrigger cronTriggerItem:[Select Id From CronTrigger where  
                                    NextFireTime= null  AND State='DELETED' Limit 100]){
                System.abortJob(cronTriggerItem.id);
        }       

Please let us know if this will help u
venkyyyvenkyyy
Sorry to saying this amit,
I did not find any difference with previous code.