function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sumit dsumit d 

error in batch :- too many callouts

hi All,
 my batch is given below:-
public class BatchZendeskToSalesforce implements Database.Batchable<sObject>{
     public Database.querylocator start(Database.BatchableContext BC){
          string query;
         
         query = 'SELECT Id, Name,url__c,External_Id__c,'+
             'Group_Id__c,Shared_Tickets__c,'+
             'Shared_Comments__c,phone,Domain_Name__c,Notes__c,'+
             'Payroll_Vault__c,Zendesk_Id__c,Details__c ' +
             'FROM Account ';
         return Database.getQueryLocator(query);
    }
     public void execute(Database.BatchableContext BC, List<Account> Accounts){
        
        Set<Id> accountIds = new Set<Id>();
        for( Account acc : Accounts ){
           accountIds.add( acc.Id );   
        }
        getAccounts();
    }
    
    public static List<Account> getAccounts(){

        HttpRequest req = new HttpRequest();
        req.setMethod( 'GET' );
        String username = 'kevin@sfdcconsultants.com';
        String password = '45j2JpHB^qy*';
        Blob headerValue = Blob.valueOf( username + ':' + password );
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode( headerValue );
        req.setHeader( 'Authorization', authorizationHeader );
        req.setHeader( 'Content-Type', 'application/json' );
        req.setEndpoint( 'https://timerack.zendesk.com/api/v2/organizations.json' );
        Http binding = new Http();
        HttpResponse res = binding.send( req );
        
        Map<String, Object> results = ( Map<String, Object> )JSON.deserializeUntyped( res.getBody() );
        List<Object> lstOrganizations = ( List<Object> )results.get( 'organizations' );
        List<Map<String, Object>> customerAtt = new List< Map< String, Object >>();
        for ( Object customer : lstOrganizations ) {
            Map<String, Object> customerAttributes = ( Map< String, Object >)customer;
            customerAtt.add( customerAttributes );
        }
        List< Account > listAccountToUpsert = new List< Account >();
        for( Map< String, Object> attMap : customerAtt ){
            Account acc = new Account();
            String fullName = String.valueOf( attMap.get( 'name' ));
           /* if( fullName.split(' ').size() > 1 ){
                ct.FirstName = fullName.substring( 0, fullName.indexOf(' ') );
                ct.LastName = fullName.substring( fullName.indexOf(' ') + 1 );
            }else{
                ct.LastName = fullName;
            }*/
            //ct.email = String.valueOf( attMap.get( 'email' ));
            acc.url__c =  String.valueOf( attMap.get( 'url' ));
            acc.External_Id__c = String.valueOf( attMap.get( 'external_id' ));
            acc.Group_Id__c = String.valueOf( attMap.get( 'group_id' ));
            acc.Shared_Tickets__c = Boolean.valueOf( attMap.get( 'shared_tickets' ));
            acc.Shared_Comments__c = Boolean.valueOf( attMap.get( 'shared_comments' ));
            acc.phone = String.valueOf( attMap.get( 'phone' ));
            acc.Domain_Name__c = String.valueOf( attMap.get( 'domain_names' ));
            acc.Notes__c = String.valueOf( attMap.get( 'notes' ));
            acc.Payroll_Vault__c = String.valueOf(attMap.get('payroll_vault'));
            acc.Zendesk_Id__c = String.valueOf( attMap.get( 'id' ));
            acc.Details__c = String.valueOf(attMap.get('details'));
            
            //ct.Zendesk_Parent_Organisation_Id__c = String.valueOf( attMap.get( 'organization_id' ));
            listAccountToUpsert.add(acc);
            /*if(ct.Name == 'testBC'){
                System.assert(false, listAccountToUpsert);
            }*/
        }
        return listAccountToUpsert;
 }
     public void finish(Database.BatchableContext BC){
        
    }
}
when i run the batch its showing me error:- too many callouts
how to solve this error?
Any suggestions?
 
@Amit Kumar Giri@Amit Kumar Giri
First - Include callout in class defination.
public class BatchZendeskToSalesforce implements Database.Batchable<sObject>,Database.AllowsCallouts{
//Code
}
Second - reduce your batch size. max callout in apex is 10 for one transaction . so u need to execute ur batch with a limit
Database.executeBatch(new MyBatchClass(), Limits.getLimitCallouts());
** Limits.getLimitCallouts() will return the total number of Web service statements that can be processed

 
Asif Ali MAsif Ali M
@sumit, did you forget to remove username/password from your code? 
sumit dsumit d
hi All,
i have change the batch but it didnt create the zendesk Account on salesforce?
batch is given below:-
public class BatchZendeskToSalesforce implements Database.Batchable<sObject>,Database.AllowsCallouts, Database.Stateful{
     public Database.querylocator start(Database.BatchableContext BC){
          string query;
         
         query = 'SELECT Id, Name,url__c,External_Id__c,'+
             'Group_Id__c,Shared_Tickets__c,'+
             'Shared_Comments__c,phone,Domain_Name__c,Notes__c,'+
             'Payroll_Vault__c,Zendesk_Id__c,Details__c ' +
             'FROM Account ';
         return Database.getQueryLocator(query);
    }
     public void execute(Database.BatchableContext BC, List<Account> Accounts){
        
        Set<Id> accountIds = new Set<Id>();
        for( Account acc : Accounts ){
           accountIds.add( acc.Id );   
        }
        getAccounts();
    }
    
    public static List<Account> getAccounts(){

        HttpRequest req = new HttpRequest();
        req.setMethod( 'GET' );
        String username = 'kevin@sfdcconsultants.com';
        String password = '45j2JpHB^qy*';
        Blob headerValue = Blob.valueOf( username + ':' + password );
        String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode( headerValue );
        req.setHeader( 'Authorization', authorizationHeader );
        req.setHeader( 'Content-Type', 'application/json' );
        req.setEndpoint( 'https://timerack.zendesk.com/api/v2/organizations.json' );
        Http binding = new Http();
        HttpResponse res = binding.send( req );
        
        Map<String, Object> results = ( Map<String, Object> )JSON.deserializeUntyped( res.getBody() );
        List<Object> lstOrganizations = ( List<Object> )results.get( 'organizations' );
        List<Map<String, Object>> customerAtt = new List< Map< String, Object >>();
        for ( Object customer : lstOrganizations ) {
            Map<String, Object> customerAttributes = ( Map< String, Object >)customer;
            customerAtt.add( customerAttributes );
        }
        List< Account > listAccountToUpsert = new List< Account >();
        for( Map< String, Object> attMap : customerAtt ){
            Account acc = new Account();
            String fullName = String.valueOf( attMap.get( 'name' ));
           /* if( fullName.split(' ').size() > 1 ){
                ct.FirstName = fullName.substring( 0, fullName.indexOf(' ') );
                ct.LastName = fullName.substring( fullName.indexOf(' ') + 1 );
            }else{
                ct.LastName = fullName;
            }*/
            //ct.email = String.valueOf( attMap.get( 'email' ));
            acc.url__c =  String.valueOf( attMap.get( 'url' ));
            acc.External_Id__c = String.valueOf( attMap.get( 'external_id' ));
            acc.Group_Id__c = String.valueOf( attMap.get( 'group_id' ));
            acc.Shared_Tickets__c = Boolean.valueOf( attMap.get( 'shared_tickets' ));
            acc.Shared_Comments__c = Boolean.valueOf( attMap.get( 'shared_comments' ));
            acc.phone = String.valueOf( attMap.get( 'phone' ));
            acc.Domain_Name__c = String.valueOf( attMap.get( 'domain_names' ));
            acc.Notes__c = String.valueOf( attMap.get( 'notes' ));
            acc.Payroll_Vault__c = String.valueOf(attMap.get('payroll_vault'));
            acc.Zendesk_Id__c = String.valueOf( attMap.get( 'id' ));
            acc.Details__c = String.valueOf(attMap.get('details'));
            
            //ct.Zendesk_Parent_Organisation_Id__c = String.valueOf( attMap.get( 'organization_id' ));
            listAccountToUpsert.add(acc);
            /*if(ct.Name == 'testBC'){
                System.assert(false, listAccountToUpsert);
            }*/
        }
        return listAccountToUpsert;
 }
     public void finish(Database.BatchableContext BC){
        
    }
}
Any suggestions ? 
how to solve it?
suresh beniwal 21suresh beniwal 21
Hi Sumit,

If you are scheduling the batch or calling from Apex, please specify the batch size as shown below. 
 
Database.executeBatch(new BatchZendeskToSalesforce (), 2);

Thanks,
Suresh Beniwal