I want to simulate / fake a long running transaction context - does anyone have any code which I can use to artificially extended my transaction context in a controlled way?


  • is this in a test class, in visualforce, or in 'normal' apex? Nov 25, 2016 at 9:31
  • I am building out the Web Service framework piece for complex algorithm based project. There will be scenarios where we cannot calculate the results quick enough and within limits. Maybe it is a mute question as we will probably queue the request immediately and give them an Id which they can then make another REST request to check status of calculation. I was more curious to see if I can fake time Nov 25, 2016 at 9:39

1 Answer 1


There is no official 'sleep' function in APEX, but a way to do this would be to make a call to a dummy service which will sleep a requested amount of time.

The drawback of course is that this requires an external callout.. Any 'internal' pause is not supported by Salesforce because it will use up CPU time need for other 'tenants'. So redesigning your logic to prevent needed sleep is usually a better option.

There are existing public services that offer this functionality, e.g. http://1.cuzillion.com/bin/resource.cgi.

Don't forget to Configure the Remote Site in SalesForce (Security Controls -> Remote Site Settings).

APEX method:

public static void sleep(Integer numberOfSeconds) {
    Long startTimestamp = System.currentTimeMillis();
    HttpRequest req = new HttpRequest();
    req.setEndpoint('http://1.cuzillion.com/bin/resource.cgi?sleep=' + numberOfSeconds);
    Http http = new Http();
    HTTPResponse res = http.send(req);
    System.debug('Duration: ' + (System.currentTimeMillis() - startTimestamp) + 'ms');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.