+ Start a Discussion
Deepak PansariDeepak Pansari 

Want time delay between two function call in apex code

Hi All,

 

I need to call two functions and want a delay of 10 seconds between then.

 

It is possible with Apex code ?

 

Thanks,

Deepak

bob_buzzardbob_buzzard

One thing to be aware of - actionFunction is a visualforce component rather than Apex code (although it does case apex code to be executed).

Deepak PansariDeepak Pansari

Hey Thanks Umesh,

 

I want same thing in Apex code. I am not using any VF pages.

 

Please let me know if we have any similer method in Apex code also.

 

Thanks for your reply.

 

- Deepak

bob_buzzardbob_buzzard

I don't believe this is possible in Apex code.  There is no mechanism to put code to sleep for any period of time, and I doubt you'll be able to schedule another class to execute with that level of accuracy.

shruthishruthi

You can try faking by going into a while loop for 1 second like this:

 

Integer start = System.Now().millisecond();
while(System.Now().millisecond()< start+1000){ 
}

 

bob_buzzardbob_buzzard

You'll stand a good chance of breaking the maximum script statement limit that way - I'd be wary of relying on that technique.

shruthishruthi

Bob Buzzard: You are right. There is the concern of max script statements.

SuperfellSuperfell

What are you trying to acheive by the 10 second delay ?

Deepak PansariDeepak Pansari

Hey

 

I am having requirment , which are using Salasforce to saleforce connection , and object are coming from one Org to secomd Org , so at some point of time i need some delay..

 

- Deepak

jkucera2jkucera2

Simon - I have the same need, where I get a callout error if I hit a 3rd party API too quickly, so I need to build in error handling to retry an @future method callout 1 sec later.  

 

Between the 10 callout / request limit, 10 scheduled job limit, 1 callout / batch apex method limit, & 5 concurrent batch jobs limits, I have no other way to.  Their API isn't great as I need to make ~240 calls for 1 full "request" (x up to 50 request for an initial load), so there's nothing I can do on that side other than sacrifice functionality, which is a bad solution.  

 

Example process flow:

1) Click button, 1 callout

2) Process results, make 2nd callout

3) Process results, make 3rd callout

4) Process results, call batch job to make ~100 callouts

5) Loop over step 3 array, make up to 10 @future calls

6) in @future, make a callout

7) Process results, make another callout

8) Process results, save record ID's to a custom object for later processing

9) At some later time, call batch job to process custom object details

 

Ste 6 is breaking given multiple @future can process concurrently.  

Starz26Starz26

For those that want to slow down or ensur that a call completes first:

 

Not sure if this will work in practice but in theory it might....

 

Create a global class with a variable that is static.

 

In the apex class try:

 

do{

   if(GlobalVariable == False){

             GlobalVariable = True;

             // Call Webservice class here      

 }

} while (GlobalVariable == True)

 

Then in the Class that calls the webservice, upon completion, set the GlobalVariable = False.

 

You should add a failsafe to exit the Do after x number of script statements just in case

 

Just a thought...

 

bob_buzzardbob_buzzard

I think you stand quite a good chance of breaching the 200,000 script statements governor limit here, and I doubt it would be viewed as a good practice by Salesforce. 

Starz26Starz26

Yup, Agreed Bob...

 

That is why I added the part about checking the script limits and exiting the Do-While-Loop prior to hitting the limit maybe after say 20,000 script statements or so..

 

Starz26Starz26

You could also use inbound email processing and have the call / method / whatever send an email upon completion and when the email is received, fire of the next process you wish to call.

hisalesforcehisalesforce

Hi ,

I also need this delay concept for salesforce to salesforce connection.

I tried to called future class 10 times as its a limit .

Still its not enough to make the delay.

I am sure i am doing wrong over here.Pleas if you can provide sample code.

 

Thanky you 

MyGodItsColdMyGodItsCold

I like the inbound email processing approach - linking several together - perhaps using a user field as a counter - having it recurse enough times to get to your 10 seconds? I'm about to prototype - I also need this - I'm trying to email an Attachment, then delete it - the @future code is too fast for larger Attachments ;-(

G2WIntegrationG2WIntegration

Just an idea, but in the next release (Oct '12), you can queue a batch job from a batch job's finish method.  If you can call the same class, that could be a poor man's delay as it waits for the queue as long as you build in a circuit breaker to prevent an infinite loop.

S Thomas-3S Thomas-3
public static sleep(Long milliSeconds)
{
        Long timeDiff = 0;
        DateTime firstTime = System.now();
        do
        {
            timeDiff = System.now().getTime() - firstTime.getTime();
        }
        while(timeDiff <= milliSeconds);
}

If it is in a future method, it wont make your tranaction hanged.
HariPHariP
Hi,

I have 2 future job running simultaneously and at the end of the job, they update 1 field on same object. Each tries to replace portion of string and keeps the other value. Since both tring to update the record around same time, which ever updates last, keeping the other value, but the other value cleared by other future method. I am querying record before replacing the value and updating, but still no luck.

So I added some fake time gap while calling one of the future method, but it is giving CPI time limit exceeded error.

Anyone have any idea?

Thanks
 
Juliana Pa 9Juliana Pa 9

Integer start = System.Now().millisecond();

while(System.Now().millisecond()< start+10) {

 }

only if you are using visual force page. In Apex there is no function to delay the code execution

Roger MorrisRoger Morris
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_continuation_chained_callouts.htm
Mark Keckeis 34Mark Keckeis 34
@julianapa - why wouldnt the same approach work in an Apex method?
Pankaj MehraPankaj Mehra
Hi,

I know the thread is old and I do not see any marked solution but I have created a wait in my apex code using schedulable batches. You can schedule your batch class to run after some seconds, in this way you can have a new thread running after a specified time where you can query records to see details. You still might have to handle limits like concurrent batches and some other.