+ Start a Discussion
RajnisfRajnisf 

Too many callouts: 101 urgent help needed

m trying to callout thru schedulable and batch class...m stuck in  Too many callouts: 101 ...plz help
public class SchedulerForPostApex implements Schedulable{
    public void execute(SchedulableContext sc){
        system.debug('*******Going to call future method ');
        Postbatchclass  batchapex = new Postbatchclass();
        id batchprocessid = Database.executebatch(batchapex,1);
system.debug('Process ID: ' + batchprocessid);
  
        
    }
}

----------------------------------------------------------------------

global class Postbatchclass implements Database.Batchable<sObject>,Database.AllowsCallouts {

Public String query;
    
    global Database.QueryLocator start(Database.BatchableContext BC)
    {
        query = 'select id, Name, SOCi_Id__c FROM Account where SOCi_Id__c != null'; //need to define query
        return Database.getQueryLocator(query);

  
    }
     global void execute(Database.BatchableContext BC, List<Account> scope) {
     
         FOR(Account sr : scope){
           PostsRequest.getpostsCalloutResponseContents(sr);
       }
         
      }
      global void finish(Database.BatchableContext BC){
      }
}

-------------------------------------------------------------------------------------------------------------------------

public class PostsRequest { 
    
    private static final String API_KEY = 'apikeyy';     
    
    //@future(callout=true)
    public static void getpostsCalloutResponseContents(Account ac) {  
    Http h;
    HttpRequest req;
    HttpResponse res;

    //String finalResult = '';
    Map<String, List<PostClass>> WrapperPostMap = new Map<String, List<PostClass>>();
    List<Account> alist = [select id, Name, SOCi_Id__c FROM Account where SOCi_Id__c != null];  
    List<PostClass> WrapperPostList = new List<PostClass>(); 
    for (Account a : alist) {
        String result = null;                        
        h=new Http();
        req=new HttpRequest();
        req.setEndpoint('endpoint' + a.SOCi_Id__c + '/get?since=2011-01-01&api_key=' + API_KEY);
        req.setMethod('GET');
        res = h.send(req);
        if(res.getStatusCode() == 200){
            result = res.getBody();
          }        
      
            
        List<Object> resultList = (List<Object>) JSON.deserializeUntyped(res.getBody());              
      
        if(resultList != null){
          for (Object entry : resultList) {
                 WrapperPostList.add((PostClass)(JSON.deserialize(JSON.serialize(entry),PostClass.class)));
            }
          }
                WrapperPostMap.put(a.id, WrapperPostList);
          
                System.debug('The WrapperPostListid List value size is: ' + WrapperPostList.size()); 
      h=null;
        req=null;
        res=null;    
    
      } 
        
         System.debug('The WrapperPostListid List value is: ' + WrapperPostList);
         System.debug('The WrapperPostListid List value size is: ' + WrapperPostList.size());
        
        for (Account a : alist) {                
              makeCallout(WrapperPostMap.get(a.Id), a.id);
          }
        
    }    
  
    public static void makeCallout(List<PostClass> WrapperPostList, String aid) {
        String result = null;       
                  
       
      
             System.debug('The WrapperPostList list is: ' + WrapperPostList); 
        Set<String> postIds = new Set<String>();
        for (PostClass f : WrapperPostList) {
            postIds.add(f.id);
        }

        Map<String, Post__c> posts = new Map<String, Post__c>();
        Map<String, Post__c> newposts = new Map<String, Post__c>();
        for (Post__c p : [
                select Id, Name, SOCi_Post_Id__c
                from Post__c
                where SOCi_Post_Id__c in :postIds
                ]) {
            posts.put(p.SOCi_Post_Id__c, p);
        }


        for (PostClass f : WrapperPostList) {
            Post__c p;
            if (posts.containsKey(f.id)) {
             
            } else {
                
                p = new Post__c(SOCi_Post_Id__c = f.id, Account__c = aid);
                p.Name = f.network +' '+ 'Post';
                p.Date_Scheduled__c = Date.valueOf(f.created_at);
                p.Created_By__c = f.created_by_name;
                p.Post_Date__c = Date.valueOf(f.schedule);
              //  p.Channel__c=f.remote_network_id;
              //  p.Channel__c = [SELECT id from Channel__c where Account__r.SOCi_Id__c =: f.project_id and Type_of_Channel__c =: f.network LIMIT 1].id;
        newposts.put(f.id, p);
            }
            
          
        }

     insert newposts.values();
                     
  }
  
   
    class PostClass {
        public String id;
        public String project_id;
        public String network;
        public String created_by_name;
        public String schedule;
        public String created_at;
       // public String remote_network_id;
        
    }


 
}
Hemant_JainHemant_Jain
Try changing the below line in execute method.
 
id batchprocessid = Database.executebatch(batchapex,1);

to this
id batchprocessid = Database.executebatch(batchapex,100);

Replacing 100 instead of 1. Let me know​ if this helps.
RajnisfRajnisf
Two batches worked fine...
one had problem...
SerialBatchApexRangeChunkHandler
You have uncommitted work pending. Please commit or rollback before calling out