function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Sandesh D GanjareSandesh D Ganjare 

How to insert delay in apex class?

Hi,
I am new to salesforce. I have an apex class from where I am calling a web service. I want to insert the delay of 10 sec so that I can handle the 'over query limit' error. 
thanks in advance!
Chandra Sekhar CH N VChandra Sekhar CH N V
In Apex there is no function to delay the code execution. If its a batch apex, Use optional param of the method that kicks off the batch. Database.executeBatch(new MyBatchClass(), 10); will walk through "whatever comes from query locator" in chunks of 10 records at a time.

Check if future annotation can be added for your scenario - 
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_annotation_future.htm
Stuart FlemingStuart Fleming
This might work, but you might get other errors

public static void sleep(Integer secs ) {
datetime myStartDateTime = datetime.now();
DateTime newDateTime = myStartDateTime.addSeconds(secs);
while (datetime.now() < newDateTime ) {
// sleeping here...sort of
}
datetime myEndDateTime = datetime.now();
System.debug('start:  ' + myStartDateTime ) ;
System.debug('end:  ' + myEndDateTime) ;
}