+ Start a Discussion
harry63harry63 

too many future calls

hI i had an asynchronous class contains two methods.so i used to @future calls for those. i am posting my trigger code and asynchronous class.could you please help me.

Thanks in Advance

trigger code

trigger open120 on Opportunity (before insert, after update) 
{

 if(trigger.isinsert){
list<String> OpportunityAccountIds = new list<String>(); 
for(opportunity opp:trigger.new)
{OpportunityAccountIds.add(opp.Accountid);}
opp120.opp120Method(OpportunityAccountIds);}

else{
list<String> OpportunityAccountIds = new list<String>(); 
list<String> OpportunityOldAccountIds = new list<String>(); 
for(opportunity opp:trigger.new)
{OpportunityAccountIds.add(opp.Accountid);}
for(opportunity opp:trigger.old)
{OpportunityOldAccountIds.add(opp.Accountid);}

opp120.opp120Method(OpportunityAccountIds);  //getting error too many future calls:11 at this line
opp120.opp120OldMethod(OpportunityOldAccountIds);}


}

 

 

asynchronous class

public class opp120{
@future(callout=true) 
public static void opp120Method(list<String> OpportunityAccountIds){ 
list<Opportunity>lstOpp =[select Id, StageName,Expiry_Date__c from Opportunity where AccountId IN :OpportunityAccountIds];  

for(Account ac:[select id,No_of_Open_Deals_in_Next_120_Days__c from Account where id in :OpportunityAccountIds])
{
integer i=0;
 for(Integer j = 0; j < lstOpp.size(); j++)  {
if((lstopp[j].StageName=='Prospecting'||lstopp[j].StageName=='Renewal'||lstopp[j].StageName=='Submission')&&(lstopp[j].Expiry_Date__c>=System.today()+120))
{
i++;
}
ac.No_of_Open_Deals_in_Next_120_Days__c=i;
}update ac;
}
}
@future(callout=true)
public static void opp120OldMethod(list<String> OpportunityOldAccountIds){ 
list<Opportunity>lstOpp = [select Id, StageName,Expiry_Date__c from Opportunity where AccountId IN :OpportunityOldAccountIds];  

for(Account ac:[select id,No_of_Open_Deals_in_Next_120_Days__c from Account where id in :OpportunityOldAccountIds])
{
integer i=0;
 for(Integer j = 0; j < lstOpp.size(); j++)  {
if((lstopp[j].StageName=='Prospecting'||lstopp[j].StageName=='Renewal'||lstopp[j].StageName=='Submission'||lstopp[j].StageName=='ClosedLost'||lstopp[j].StageName=='ClosedWon')&&(lstopp[j].Expiry_Date__c>=System.today()+120))
{
i++;
}
ac.No_of_Open_Deals_in_Next_120_Days__c=i;

}update ac;
}
}

}

 

 

jhenningjhenning

There is a limit of a maximum of 10 asynchronous calls within the context of a simgle Apex logical transaction. You are hitting this limit because your trigger calls the method more than ten times.

harry63harry63

HI tHANKS FOR REOLY...COULD YOU MODIFY MY CODE TO OVERCOME THE ERROR.

THANKS

HARRY

Alex WeinleAlex Weinle

Hi Harry,

 

I'm afraid thegist of what you are being told by SalesForce is that you can't make that many future calls in a single transaction, here are a few possible thoughts:

 

1) Does it really need to be in a trigger (asynchronous)? Sometimes you can make your future call directly after a page wizard or process?

2) Can you pass all the Ids in a MAP or SET into a single future call?