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
Nishad BashaNishad Basha 

How to schedule the schedulable class for every one minute in salesforce?

can you please give me the example of schedule apex class for every one minute .How to solve the above scenario.please give some ideas.
Amit Chaudhary 8Amit Chaudhary 8
Please try below code to sechedule for every one min:-

Example 1:-
String chron1 = '0 1 * * * ? *';      
System.schedule('Every 1 min', chron1, new JOBNAME());
Example 2:-

        //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());
        
        JOBNAME Job = new JOBNAME();
        system.schedule('Scheduled at '+System.now().format(), nextScheduleTime, Job );


Please let us know if this will help you.

Thanks,
Amit Chaudhary
Apoorv Saxena 4Apoorv Saxena 4

You can schedule your class by using the System.schedule method.

The code below will get your scheduled job to run every minute
String sch = '0 * * * * *';
System.schedule('Jobs scheduled name', sch, new Job());

The format for schedule is given below -->>
Seconds - Minutes - Hours - Day_of_month - Month - Day_of_week - optional_year

* Specifies all values.So it will run for every minute of every hour on every day and so on...!!

Hope this helps..!!

ManojjenaManojjena
Hi Nishad,

Try with blow cod eit wil help .
 
global class scheduledInEachMinute implements Schedulable {
   global void execute(SchedulableContext SC) {
      BusinessLogicClass blc=new BusinessLogicClass();
   }
}

scheduledInEachMinute minut = new scheduledInEachMinute();
String sch = '0 0/1 * 1/1 * ? *';
String jobID = system.schedule('Your Job', sch, minut);

You should writ eyour business logic in your BusinessLogic class .else you can write code inside your execute methos .

To Know more detail about the schedule class you can check the below link it will help .

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm  


let me know if it helps .

Write the class and execute an dto know the next schedule time you can query in workbench from the CornTrigger Object .

Let me know if it helps !!
Thanks 
Manoj
Nishad BashaNishad Basha

Hi, Manoj

How to run the scheduled job above code please give some ideas.
Amit Chaudhary 8Amit Chaudhary 8
Hi Nishad,

Please execute below code in develope console.

 //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());
        
        JOBNAME Job = new JOBNAME();
        system.schedule('Scheduled at '+System.now().format(), nextScheduleTime, Job );

That will help you.
 
ManojjenaManojjena
Hi Nishad,

Basically there is a method in system class which takes three(3) parameter one is job Name ,second  is  corn expression , and third is your instance of your schedule class .

So you can execute the code in Developer console ,Whcih will create a record in CornTrigger object .and automatically schedule the class .
//Below class should your schedule class 
scheduledInEachMinute minut = new scheduledInEachMinute();
//Below expresion to execute in each one minute 
String sch = '0 0/1 * 1/1 * ? *';
//This is to execute the schedule class and crreate record in CornTrigger for next execution .
String jobID = system.schedule('Your Job', sch, minut);
To know more detail about the execte method in System class please cehck below link
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm#apex_System_System_schedule.

Let me know incase any issue .

Thanks 
Manoj

 
Nishad BashaNishad Basha
Hi, Amit Chaudhary 8

How to write the schedule apex class for above code.please give entire code with example.
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
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());
Please let us know if this will help you.

Thanks,
Amit Chaudhary
Nishad BashaNishad Basha
Hi, Amit Chaudhary 8

How to schedule an Apex schedulable class to run for 10 days before in salesforce?

Here i want to schedule the 10 days before data.How to run the schedule job please give me the example of above scenario. please give some ideas.
Nishad BashaNishad Basha

Hi, Amit Chaudhary 8

How to query all the account created before 10 days and update their names.
 
Nishad BashaNishad Basha

Hi, Manoj

How to schedule an Apex schedulable class to run for 10 days before in salesforce?

                     can you please give me the example of above scenario.please give some ideas.
 
Amit Chaudhary 8Amit Chaudhary 8
Please try below code:-
global class TestSchedulableClass implements Schedulable 
{
    global void execute(SchedulableContext ctx) 
	{           
		// add your logic here	
		DateTime dt= System.today()-10;
		List<Account> lstAcc = [select id,createdDate  from account where createdDate < = :dt ];
        System.debug('--->'+lstAcc );
		// 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 let us know if this will help u
Christina ZhankoChristina Zhanko
Hi!
Our company has a cool solution for problems like yours!
Try this https://appexchange.salesforce.com/listingDetail?listingId=a0N3A00000DqCmYUAV.
It allows you to execute your schedule at a specified frequency, even every 5 minutes.