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
Vinnie BVinnie B 

How can you prevent future functions from locking out other future functions?

I have a program that runs every morning at 4:00 am.  Since it does some CPU intensive calculations I put some of the heavy lifting into five future functions that are called from within the main program.  Things generally are working fine.

Occassionally, I'll get a message about locked rows.  This is, of course, quite bad as some of my updates just don't happen.

I did some other coding with batch files and got around this by having Batch-1 call Batch-2 which calls Batch-3, etc.  Is there some way to make sure five different future calls won't run at the same time?

Do you have to have so many @future functions?  
Vinnie BVinnie B
Sort of.  I do have one that takes up about 28K of the allowed 60K CPU time.  I did condense three of them that were less intensive into one future function that calls the same code sequentially.  This might help as I know these are updating the same set of accounts.

In the long run, I should re-work the logic that's causing the heavy CPU usage.  But that's a topic for a whole other thread.  :)
Batch up your data and call the @future function in your batched code to process less data?

Without knowing a little more info, it's kind of hard to see what needs to be done.  :)
Vinnie BVinnie B
I guess I could pull this into batch files if needed but that wouldn't be easy to convert.  I'm doing queries, creating custom data objects from those results,  doing more queries to get additional data that I need, etc.
Vinnie BVinnie B
As noted, this problem happens infrequently.  I thought about two ways this could resolved.  First, if there was a delay function that allowed the functions to run differently.  But that doesn't seem feasible as SFDC doesn't want idle threads sitting around waiting to run later.

Secondly, I was hoping there was a way to code Apex such that it would try the query again if it first couldn't create the lock on the rows in the table.  I could show you all of my code, but I think my problem is a more generic one such that seeing my code wouldn't help much.

THANKS for your help!!