+ Start a Discussion
GoForceGoGoForceGo 

Governor Limits, @future - How do I know I have more than a 1000 contacts?

I have an Account trigger where I want to update all the contacts.

 

I might have more than a 1000 contacts (query limit) or 100 contacts (DML Limit) So I might have to resort to a @future method. 

 

However given that @future methods have 200 a day limit (per user or per org?), I don't want to call a @future method if an Account has only 2 contacts.

 

But how do I find out that I have only 2 contacts? I have to query, and that might cause a governor limit error...

 

 

mattdarnoldmattdarnold

My understanding is that it is 200 @future calls per user per day. Would this work: first test the number of records in the query with select count() (put some limit on this to avoid a governor limit) and then if that is above 100 records, call the @future method with the account ID as a parameter, otherwise execute your update in the trigger. Some example code (untested):

 

trigger updateContacts on Account (after update) {

Set<String> accountIds = new Set<String>();
for(Account a : Trigger.new) {
accountIds.add(a.id);
}

Integer contactCount = [select count() from Account where id in: accountIds limit 1000];

if(contactCount > 100) {
// Call @future method with accountIds as a parameter
} else {
// Run a query with accountIds as a parameter and update those contacts
}
}

 

 

 

GoForceGoGoForceGo

I think this should work.


Needless to say, I would set the limit as 101, not 1000 for the test, since I can't update more than a 100 records anyway.

 

Thanks!

 

 

 

Message Edited by GoForceGo on 07-24-2009 12:44 PM
jeffdonthemicjeffdonthemic

You also might want to take a look at the Limits methods as there are some calls pertaining to @future (e.g., getFutureCalls, getLimitFutureCalls).

 

HTH

 

Jeff Douglas

Appirio

http://blog.jeffdouglas.com