Friday, August 7, 2015

Resolution for Too many future calls limit in Apex

In Sales force we do have many limits one among them is @future calls(asynchronous). Salesforce has a limit of 50 future calls for a single transaction.

Salesforce has introducted Queueable interface supersede the old @future annotation in Apex. now you can chain a job to another job an unlimited number of times.

Trigger
trigger SendAccount on Account(after insert)
{ 
 for(Account a : Trigger.new)
  {
 SendAccountUsingRESTAPI.callcreateAcc(a.Name, a.Id);
  }
}
SendAccountUsingRESTAPI Apex Class
public class SendAccountUsingRESTAPI {
  @future(callout=true)
   public static void callcreateAcc (String accName, String accId)
   {
  System.debug('Created Account Name:'+accName);
  System.debug('Created Account Id:'+accId);
   }
}

I tried to call future method 50 times below is code. Execute the code in developer console or on the Apex Execute on Workebench

List<Account> lstAccount=new List<Account>();
for(Integer i=0;i<51;i++){
 Account acc=new Account();
 acc.Name= 'Test Sample'+i;
 lstAccount.add(acc);
}
insert lstAccount;
I modifed the trigger and created a new class AccountQueuebleJob which implements Queueable. I ran the same code above eeverything when fine.
trigger SendAccount on Account(after insert)
{
  List<Account> lstAccount =new List<Account>();
  for(Integer i=0;i<Trigger.new.size();i++){
      if(i<50){
          SendAccountUsingRESTAPI.callcreateAcc(Trigger.new[i].Name, Trigger.new[i].Id);
      }else{
          lstAccount.add(Trigger.new[i]);
      }
  }
  
  if(!lstAccount.isEmpty()){
      ID jobID = System.enqueueJob(new AccountQueuebleJob(lstAccount));
  }
}
AccountQueuebleJob Class
public class AccountQueuebleJob implements Queueable, Database.AllowsCallouts {

    private List<Account> lstAccount;
    
    public AccountQueuebleJob(List<Account> lsacc){
        this.lstAccount = lsacc;
    }

    public void execute(QueueableContext context) {
        if(!lstAccount.isEmpty()){
            for(Account a:lstAccount){
                SendAccountUsingRESTAPI.callcreateAcc(a.Name, a.Id);
            }
        }
        
    }
}

7 comments :

  1. Hi Suresh,
    is this solution working for for you ?. I tried same solution but I am facing too many future limits because limit is 50 per transaction. Here is my sample code.


    public class TempQueueableTest implements Queueable {
    // implements Queueable

    public void execute(QueueableContext context) {
    for(integer i = 0; i <= 60; i++){
    calcCommunityRollups(commID);
    }
    }

    @future
    public static void calcCommunityRollups(id communityID) {
    System.debug('This is for testing');
    }

    }

    ReplyDelete
  2. Thank You Blogger for such a Beautiful Blog.Your Blog Design Perfect Report and Level Too Thanks By Research Panel.
    Free Trial

    ReplyDelete
  3. Hi, I implmented this solution but I face the same limitation with Queue : too many queueable jobs added to the queue : 51.

    ReplyDelete
  4. It is not working

    ReplyDelete
  5. This is great, although I realised that you can't call a future method from a Queueable class, and so had to remove the @Future(callout=true) from my callout method. I then experienced a further limit "Too many callouts: 101", and so I called the Queueable calls multiple times, each time with list of 100 records, and this works great. Thanks again and all good wishes. Paul

    ReplyDelete

Labels

visualforce page ( 13 ) apex integration ( 5 ) apex trigger ( 4 ) csv file from vf page ( 4 ) javascript ( 4 ) csv visualforce page ( 3 ) Too many ( 2 ) call out ( 2 ) integration ( 2 ) rest api ( 2 ) salesforce rest api ( 2 ) salesforce to salesforce integration ( 2 ) sfdc rest api ( 2 ) trigger ( 2 ) 15 digit to 18 digit ( 1 ) DML rows in Apex ( 1 ) Date Conversion ( 1 ) Date/Time conversion ( 1 ) Deploy ( 1 ) Objects to Future Annotated Methods ( 1 ) SFDC limits ( 1 ) Sobject to Future Annotated Methods ( 1 ) Test Class ( 1 ) TimeZone Conversion ( 1 ) Too many dml rows ( 1 ) Too many future calls ( 1 ) annotations ( 1 ) apex code ( 1 ) closed opportunities ( 1 ) commit ( 1 ) convert ( 1 ) create records ( 1 ) csv create records ( 1 ) custom setting ( 1 ) deployment ( 1 ) deployment changeset ( 1 ) disable apex class ( 1 ) disable apex trigger ( 1 ) disable in production ( 1 ) document ( 1 ) download ( 1 ) field name ( 1 ) formula fields ( 1 ) iframe ( 1 ) inactive ( 1 ) intellisense ( 1 ) jsforce ( 1 ) limits ( 1 ) matrix report in vf page ( 1 ) multi select ( 1 ) multi select salesforce ( 1 ) multiselect ( 1 ) paypal ( 1 ) picklist ( 1 ) record type ( 1 ) rollback ( 1 ) salesforce limits ( 1 ) salesforce list ( 1 ) salesforce map ( 1 ) salesforce rest ( 1 ) salesforce set ( 1 ) salesforce1 ( 1 ) sandbox deployment ( 1 ) sfdc collection ( 1 ) sfdc list ( 1 ) sfdc map ( 1 ) sfdc rest ( 1 ) sfdc set ( 1 ) uncommitted ( 1 ) updated field ( 1 ) user ( 1 ) validation rule opportunity ( 1 ) validation rules opportunities ( 1 ) vf page ( 1 )

Ad