I just ran into a huge gotcha in Salesforce: the 24 hr limit on # of future calls - cf. http://www.sundoginteractive.com/index.php?/sunblog/posts/governor-limit-gotchas-part-1-the-future-call/.
I have the following questions about how to handle this:
How can I determine how many future calls have been made in a 24 hour period? I need to have a trigger check first so that it doesn't cause the overall upsert operation to fail on SF records if I'm over the limit. I saw a post on Developerforce boards saying it was stored in AsyncApexJobs object, but I didn't see how to query it in the API docs.
What's the best way to have a custom script that I do to update a bunch of records in SF ensure that it is not making any future calls? Is there a way I could set some kind of global variable in my script that i could then reference in all the triggers so they wouldn't make future calls if I'm doing a mass update (it's an update of over 2000 records).
Currently I am in a really awkward situation where none of the leads from my external systems are getting added b/c of the future call failing in a trigger set to run whenever a Lead is upserted.
Here is the script that does the sync to Salesforce: https://github.com/techmission/mdl_custom_reports/blob/master/sf_student_sync.php. It is using an upsert function that leverages the PHP Toolkit, and the function is in the same Github repo, in sf_libs.inc
The code of the trigger is here:
trigger CityvisionUpsertLead on Lead (after insert, after update) { // score & qualify all leads // methods execute in future, so as to be non-blocking for save, and so tests will pass // confirm we are not in a future context or a batch context (i.e., when doing a bulk update) if(!System.isFuture() && !System.isBatch()) { Set leadIds = new Set(); // leads to score and qualify - currently filtered in the methods, not here // iterate over all the leads for (Lead l : Trigger.new) { // add all to the pool for scoring and qualification leadIds.add(l.Id); } // invoke the methods for scoring and qualifying leads // if this is not being called from a future method context if(!leadIds.isEmpty()) { CityvisionUtils.scoreLeads(leadIds); CityvisionUtils.qualifyLeads(leadIds); } } }