3

How to run a scheduled job every 1 minute , and automatically start the re-scheduled automatically again and again same process. I am trying this example below way. But it can't reschedule automatically and time interval also not clear. Could you please give me an example another way.

Global class SchedulebatchAccountUpdate1 Implements Schedulable {
    global void execute(SchedulableContext sc) {
    batchAccountUpdate1 c = new batchAccountUpdate1();
    Database.executeBatch(c);
    }

 Public static void SchedulerMethod() {   
     String sch = '0 0 * * * ?';
    system.schedule('My Jobs scheduled name', sch, new SchedulebatchAccountUpdate1());

   }
}
6
  • I think there is minimum 15 minute limit. Commented May 26, 2016 at 12:18
  • 1
    Could you post the code for your batch job? Jobs can't be scheduled to run this closely together. The next job should be called or "scheduled" to run from a method at the end of your batch class either by rewriting it using "implements schedulable" or as a queueable class. There's no guarantee when an asynchronous job will run when scheduled, only that it will run as soon as resources become available after the scheduled time it's to run.
    – crmprogdev
    Commented May 26, 2016 at 12:18
  • @crmprogdev, Could you please check the above code
    – magi 001
    Commented May 26, 2016 at 12:25
  • 1
    Scheduling a job to run every minute is a bad idea, and will likely not end well. Have a look at this question and answers salesforce.stackexchange.com/questions/13069/… Commented May 26, 2016 at 12:28
  • @ Phil Hawthorn, let me know is there any possibility or not?
    – magi 001
    Commented May 26, 2016 at 12:34

2 Answers 2

3

It is not possible. Scheduled classes are queued, and sometimes they can take longer than a minute to run. This means that your code wouldn't be accurate from the start. Also scheduled classes with little time between executions can cause bugs in your organization (I lost two sandboxes because a class would get stuck and wouldn't be able to be edited or executed again).

You should get another way to do what you want to do. If you absolutely need to do this by the minute, while your users are logged in the system, might I suggest to call your code not with a scheduled class but with a custom Javascript on the sidebar? I've done this before and it works just fine (although it had a bigger interval).

2

Here's an example of how you'd do the above using a queueable only you'd need to include the details of the Execute method that's in your BatchUpdateAccount1 which aren't shown. What's below is based on code from Dan Appleman's Advanced Apex Programming 3rd Edition.

public without sharing class AsyncAppleman 
    implements queueable {

    public void execute(QueueableContext context)
    {
        // you want a class that allows you turn your queueable on and off using custom settings 
        // which is what the below references
        if(!AppConfigSupport.appEnabled) return; // On/off switch

        // Your execute method here... 


        Integer allowedgetQueueableJobs = 
            Limits.getLimitQueueableJobs() - Limits.getQueueableJobs();

        if(allowedgetQueueableJobs()<=0) 
           return;

        if(allowedgetQueueableJobs > 0)
            try
            {
                system.enqueueJob(new GoingAsync3());
            } catch(Exception ex)
            {
                // Alternate chaining mechanism such as schedule your batch job. Check limits for schedulables first
            }

    }


}
2
  • Yes it is possible to schedule apex batch schedule every minutes. Here is the code sample you can see: developer.salesforce.com/forums/?id=9062I000000ISsPQAW See the answer of Aman Gupta 161. If it helped you. Mark this answer useful
    – Aman Gupta
    Commented Mar 21, 2022 at 11:53
  • @AmanGupta You may be able to schedule batch apex to "run", but there's no guarantee that it will execute every minute. There could be many consequences to doing this kind of scheduling where record updates could be missed or happen out of the intended sequence for the reasons I've stated above.
    – crmprogdev
    Commented Jan 22, 2023 at 16:41

Not the answer you're looking for? Browse other questions tagged .