I'm trying to run Apex code that runs a little while after custom objects are created. The problem is that I have a few systems inserting and updating data when a customer places an order in an ecommerce system.

What I want to do in theory, is run code when an Order__c is created. In practice, there are related objects to the order such as many OrderItem__c or other objects that are created seconds later. Therefore, I can't run my logic during Order__c Insert.

I tried to use @future to add a slight delay, but sometimes it happens too fast so it's not reliable.

It seems like I could use Apex Scheduler to schedule running code 1 minute later for example. However, the documentation recommends against using this in a trigger since you can only schedule 100 things at once and this would break if hundreds of orders were placed at once.

2 Answers 2


A couple of options:

  • You could run a scheduled job every X minutes/hours or at particular times in the day to process batch Orders (if the processing is not time crucial). Inside your batch you'd query orders created in the last X minutes/hours and process them. If you do this, allow a minute or two gap, in case a new order comes in when the batch runs and it doesn't have all related data yet.
  • You could change the code in your external systems to update the order with a checkbox "Order Received" once all related data comes in. That way you can even have a trigger that runs immediately as soon as the order and its related data is in.
  • 1
    Or create a web service (REST) to have the external system send and ID to so it can process, or create a time based workflow on the order to check a box in 1 hour from creation. All depends on how quickly the next process needs to actually happen
    – Eric
    Jul 29, 2015 at 5:04
  • +1 for suggested option #2. Process the records when they are known to be ready. A control flag (checkbox, status value, etc.) is the easiest mechanism to use to control the processing behavior of the previously stored records.
    – Mark Pond
    Jul 29, 2015 at 5:57
  • If I run a scheduled job every X minutes, will I need to have a checkbox to indicate whether my job was done or not and then have slightly overlapping windows? Or maybe would it be OK to just have the job run a few minutes after the hour then be programmed to process the last hour. For example, running at 1:05 and knowing to process everything from 00:00:00 - 00:59:59. What do you guys think?
    – jon_wu
    Jul 29, 2015 at 10:34
  • The latter. Have the job processing the records created in the last hour + 2minutes, - 2 minutes from now (allow for new records to be excluded and process them in the next run). So if batch is running at 1:05, it should process records created > 00:03 and < 01:03, next run at 2:05 will get records created > 1:03 and < 2:03 and so on... Jul 29, 2015 at 10:39
  • 1
    That's a good idea too. My original thought was to avoid a checkbox because it's not indexed initially so I'd have to wait for the optimizer to catch on or fight with support for weeks to get them to understand that they need to help me add an index and I can't just post in a forum ;). The other reason is that avoiding DML on processed records and updating a single in memory setting should be faster (although not as important when scheduled). Since one of my tasks is to create callouts to update other systems, I'll also use scheduled batch apex to avoid hitting limits.
    – jon_wu
    Jul 29, 2015 at 23:18

There has a way to let Apex sleep for seconds,but you have to pay attention to "Apex Governor Limits".

Long startTime = DateTime.now().getTime();
Long finishTime = DateTime.now().getTime();
while ((finishTime - startTime) < 9000) {
    //sleep for 9s
    finishTime = DateTime.now().getTime();

Reference the following link:
A way to make Thread Sleep in Apex

  • 1
    Such a bad idea
    – Eric
    Jul 29, 2015 at 5:02
  • This is a bad practice and is not reliable. Nevertheless, I had tried this method once but found that too many script statements were executed for any significant delay making it not viable.
    – jon_wu
    Jul 29, 2015 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.