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

Workaround for Sleep/Pause/Wait?

We have a webservice that sometimes gets inundated with requests from an APEX trigger we have.  So, we wrote a .NET batch program to do the requests via API through SFDC with a 45 second delay between requests.  We'd like to make that a Batch APEX, however, I can't find any way to make the APEX code pause between submittals.  I tried a loop checking on the time, but that causes 200K worth of script statements which exceeds governor limits too.


Does anyone have any ideas?




I'd be very surprised if SalesForce implemented a wait or sleep method as this would hold up transactions and wouldn't be very fair on a multi-tenanted SaaS platform.


When you say the .NET batch program fires through SFDC, what do you mean? Is the inundated webservice a SFDC webservice or another host and you are using SFDC as a proxy?


It doesn't need to be transactional to where it's holding up a transaction.  They can limit it to Batch APEX.  Then it can be checked to make sure it's not in the middle of a transaction.  But we really need it because calling web services in batches can overwhelm them and that makes the application the weakest link and not truly scalable.


BTW, when I say .NET through SFDC, I mean that we resubmit some items for a custom Lead Assignment by setting the flag for it to call the Lead Assignment web service.  That service gets flooded and can't handle hundreds of requests in a 1 minute span.

I am stucked up in similar kind of problem. Did you succedded in resolving this ? Please share with me. I want to have some

knid of wait statement between the two submittals Apex batch.


Please help, its urgent







Any workaround/solution for this.

Try something like this:

Long startingTime =; // Num milliseconds since Jan 1 1970
Integer delayInMilliseconds = 1000; // One-second delay
while ( - startingTime < delayInMilliseconds)  {
            // Do nothing until desired delay has passed
This may work now because CPU time is checked and not script statements, but it wouldn’t work before. Thanx!
Call this function and pass number of seconds as the parameter. -

public static void sleep(Integer sleepSeconds) {
    Long startTS = System.currentTimeMillis();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('' + sleepSeconds);
    Http http = new Http();
    HTTPResponse res = http.send(req);
    Long duration = System.currentTimeMillis() - startTS;
    System.debug('Duration: ' + duration + 'ms');

Via -
Sales LogistixSales Logistix
Well, there is another way to avoid the governor limit on number of statements if you have a lot of Tasks or Cases in your system. These two objects are archived after a year's time, and that means queries can take longer.  Create a list called "count" (or whatever), and in the middle of your loop, use this
   count = [SELECT id FROM TASK LIMIT 50000 ALL ROWS ];
Obviously, this wastes SOQL queries and you'll need to change the limit parameter depending on whatever else you have in memory. But in busy times of the day, that select may take a second or more each time it's executed.
OF COURSE this is a wasteful practice. SFDC has stated pretty clearly they'll never implement a sleep function, and if you use this technique sparingly it's not that sinful.
Actually, this has been pretty much solved by some of Salesforce’s latest changes to limits. Script statements are not limited anymore, but CPUTime. So a FOR loop delay waiting that many seconds is indeed possible if needed. Thanx!
Shefali Bansal 1Shefali Bansal 1

How Can I achieve this in Salesforce:

try {
        Thread.sleep(1 * 1000); // Wait for a second
    } catch (InterruptedException ex) {

Mark StvenMark Stven
Can we make such a mask that comes up with this feature that we install the sleep and wake up feature in eyesleep ( like whenever someone wear it and set timer in it. It will automattically turn on on that time and wake up that man.