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?
Thanks
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It only takes a minute to sign up.
Sign up to join this communityI 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?
Thanks
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);
req.setMethod('GET');
Http http = new Http();
HTTPResponse res = http.send(req);
System.debug('Duration: ' + (System.currentTimeMillis() - startTimestamp) + 'ms');
}