+ Start a Discussion
Sfdc wonderSfdc wonder 

How to avoid too many future callouts

How to avoid Too Many Future callouts!!!!!!Help me
I want to send contact info of particler lead to my 3rd party...
dis code will fine for 9-records



my trigger is

trigger conupdater on Contact(after insert,after update,before delete)
{
    list <lead> l=[Select id,name,lastname,firstname from lead];
    BuzzBoard_Setting__c bb=[select id,Name,Partner_key__c from BuzzBoard_Setting__c Order by CreatedDate DESC LIMIT 1];
    public String mymethod;
    public String type='Contact'; 
    Map<id,id> contMap=new Map<id,id>();
  
    if(Trigger.isAfter)
    {
      for(contact cont:Trigger.new){     
         if(cont.ContactLead__c!=null){
             contMap.put(cont.id,cont.ContactLead__c);
         }
    }
 
    }  
    if((Trigger.isAfter && Trigger.isInsert))
    {
    for(contact c:Trigger.New){
        mymethod='INSERT';
        if(contMap.containsKey(c.id)){
            LeadChildsUpdater_bb.updatenotes(c.id,contMap.get(c.id),mymethod,type,bb.Partner_key__c);
        }
      
    }
    }

    if((Trigger.isAfter && Trigger.isUpdate))
    {
    for(contact c:Trigger.New){
        mymethod='UPDATE';
        if(contMap.containsKey(c.id)){
            LeadChildsUpdater_bb.updatenotes(c.id,contMap.get(c.id),mymethod,type,bb.Partner_key__c);
        }
    }
    }

    if((Trigger.isBefore && Trigger.isDelete))
    {
    for(contact cont:Trigger.old){     
         if(cont.ContactLead__c!=null){
             contMap.put(cont.id,cont.ContactLead__c);
         }
    }
    for(contact c:Trigger.Old){
        mymethod='DELETE';
        if(contMap.containsKey(c.id)){
            LeadChildsUpdater_bb.updatenotes(c.id,contMap.get(c.id),mymethod,type,bb.Partner_key__c);
        }
    }
    }
}

My Controller class is ::
 
public class LeadChildsUpdater_bb
{
public static string resmessage;
@Future(callout=true)

  public static void updatenotes(String Id,String Lead_Id,String Action,String Type,String Key)
  {
  
    HttpRequest req = new HttpRequest();
    req.setEndpoint('https://demo.mybuzzboard.com/salesforce/assetsSync.php');
    req.setMethod('POST');
    req.setBody('Id='+EncodingUtil.urlEncode(+Id, 'UTF-8')+'&Lead_Id='+EncodingUtil.urlEncode(+Lead_Id, 'UTF-8')+'&Action='+EncodingUtil.urlEncode(+Action, 'UTF-8')+'&Type='+EncodingUtil.urlEncode(+Type, 'UTF-8')+'&Key='+EncodingUtil.urlEncode(+Key, 'UTF-8'));
    Http http = new Http();
    HttpResponse res = http.send(req);
 
    if (res.getStatusCode() == 200)
    {
          
          
             System.debug('---------------------'+res.getBody());
     }
    else
     {
      System.debug('Callout failed: ' + res);
     }

}
}
Best Answer chosen by Sfdc wonder
ScriptMonkeyScriptMonkey
As Piyush said, you should bulkify things so that you can send a list to your @future, rather than calling it more than 10 times in your loop, but I would like to add that I do not believe that will help in bulk API merge processes as they don't truly bulk, they still iterate to some degree (and since you have before delete in your trigger, that will still call your trigger)

I'd suggest to be safe, some kind of error handling or checking that uses limits.GetFutureCalls() and limits.GetLimitFutureCalls() so you can at least see where you are (or use those in your debugging as you troubleshoot as well).

Thanks,

Matt / ScriptMonkey

All Answers

piyush parmarpiyush parmar
Hi,

As my first impression on the code is. Code is not bulkyfied.. 

Use list<ID> as parameter insted of singel value parameter.

Piyush
Venkat PolisettiVenkat Polisetti
The total number of Callouts that you can make in a transaction is 10 and I believe you are hitting that limit. If you are trying to load data in bulk, you might consider disabling the trigger and enable it after after your load.

Thanks,
Venkat
ScriptMonkeyScriptMonkey
As Piyush said, you should bulkify things so that you can send a list to your @future, rather than calling it more than 10 times in your loop, but I would like to add that I do not believe that will help in bulk API merge processes as they don't truly bulk, they still iterate to some degree (and since you have before delete in your trigger, that will still call your trigger)

I'd suggest to be safe, some kind of error handling or checking that uses limits.GetFutureCalls() and limits.GetLimitFutureCalls() so you can at least see where you are (or use those in your debugging as you troubleshoot as well).

Thanks,

Matt / ScriptMonkey
This was selected as the best answer