Time delay required before function call in apex code

I need the delay time before calling the function in apex code. I already created one delay method, but it does not work on hold. So, is there a way to make this work.

Thanks in advance.

+4
source share
3 answers

Probably the best way to do this is to break up your Apex code so that the part you want to execute later is in a separate method. You can then call this method from another method that has @ future annotation, or use the Apex Scheduler to schedule this code in the future. Any of these methods will cause the code to execute asynchronously after the completion of your original method (the @future method is easier to implement, but the scheduler method has the advantage of working at predictable times).

+3
source

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

First you need to configure a new remote site in SalesForce (Security Controls β†’ Remote Site Settings), but name it, but you want to, but make sure that the URL of the remote site matches the URL above and that the "Active" checkbox is selected.

After that, you can define your method in the code as follows:

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'); } 

Launch:

 sleep(1); -> 08:46:57.242 (1242588000)|USER_DEBUG|[10]|DEBUG|Duration: 1202ms 
+3
source

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

+1
source

Source: https://habr.com/ru/post/1483144/


All Articles