Want time delay before function call in apex code
Asked Answered
W

3

5

I want time delay before function call in apex code. I already created one delay method but it is not working as per expectation. So, is there any way to get this working.

Thanks in advance.

Wale answered 28/5, 2013 at 12:20 Comment(3)
As a very dirty hack you can do Integer i=0; while(i<10000){i++;}. But why do you need this delay?Dysphemia
Hi,Chiz. Yes. I have already created the delay method same as you have mentioned. In my project, I have the bulk insert for a custom object. Basically i want delay between two inserts of records. For that purpose in existing code i am calling that delay method after one insert but it is not actually delaying here.Wale
In what type of execution context are you trying to do this, and most of al .. why. In most cases this type of requirement may indicate a poor design.Nitrous
S
5

Probably a better way to do this would be to break up your Apex code such that the part you want to execute later is in a separate method. You can then call this method from another method that has an @future annotation, or use the Apex Scheduler to schedule that code for a future time. Either of these methods will cause the code to be executed asynchronously after your original method has completed (the @future method is easier to implement but the scheduler method has the advantage of running at a predictable time).

Steffin answered 29/5, 2013 at 0:10 Comment(1)
Yes. Cool idea is to schedule a job. Start this job in 5 or 10 secs and you are done.Dysphemia
B
4

If you need something like the sleep() function, one way to do it is to make a call to a http service which will sleep a requested amount of time. This is fairly simple to do, and there are existing publicly available services for it, for example the one at http://1.cuzillion.com/bin/resource.cgi.

First you have to Configure a new Remote Site in SalesForce (Security Controls -> Remote Site Settings), name it however you want but make sure the Remote Site URL matches the above URL, and that the "Active" checkbox is checked.

After that, you can define your method in code like so:

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

Running it yields:

sleep(1);
-> 08:46:57.242 (1242588000)|USER_DEBUG|[10]|DEBUG|Duration: 1202ms
Bisexual answered 29/5, 2013 at 15:3 Comment(1)
That was creative. Drawback is that you cannot do this after DML operations, right?Uchida
D
1

You can easily do this in Visualforce. Either use apex:actionPoller and set the timeout property to whatever you want the interval to be. Or use window.setTimeout(function, 1000) in JavaScript. Then from the function in JavaScript you can either use JavaScript Remoting or apex:actionFunction to call back into Apex.

Darren answered 28/5, 2013 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.