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
Angello Camacho DragoAngello Camacho Drago 

Batch class does not execute all the batches it should when calling web services in the execute method

I want to send salesforce data to another system, but when I send the data through a batch class when I call the web service in the execute method doesn't execute all the batches, but if in the execute method don't make the callout all the batches execute, I put two counters in the execute method and I am showing in the finish method one for the number of records in the list and the other for the times the execute method is running, and when I execute the batch without make the callouts run all the batches but when execute the batch calling the web service in the execute method don't run all the batches.
MythreyeeSSMythreyeeSS
Hi - When some exception/ error happens in the execute method, that transaction alone is rolled back. So if there are 1000 records with batch size 200, then 5 batches will be executing. Assume that you are incrementing the variable count for each execute method, then count variable will have value 2 if three batches failed due to some callout issues. To get detailed information on this, turn debug log on to see the error message or implement exception handling with try catch block and increment count variable outside try catch block. Thanks.
Angello Camacho DragoAngello Camacho Drago
Hi MythreyeeSS, this is my code:

global class SendFirstDonorData implements Database.Batchable<sObject>, Database.AllowsCallouts, Database.Stateful{
    public Integer i = 0;
    public Integer j = 0;
    public List<String> exceptions = new List<String>();
    
    global Database.QueryLocator start(Database.BatchableContext BC){
                   
         return Database.getQueryLocator('Select npsp__is_Address_Override__c, npsp__Soft_Credit_Last_N_Days__c, npsp__Primary_Contact__c, npsp__Primary_Affiliation__c, npsp__Number_of_Soft_Credits__c, npsp__Number_of_Soft_Credits_Two_Years_Ago__c, npsp__Number_of_Soft_Credits_This_Year__c, npsp__Number_of_Soft_Credits_Last_Year__c, npsp__Number_of_Soft_Credits_Last_N_Days__c, npsp__Last_Soft_Credit_Date__c, npsp__Last_Soft_Credit_Amount__c, npsp__Largest_Soft_Credit_Date__c, npsp__Largest_Soft_Credit_Amount__c, npsp__HHId__c, npsp__First_Soft_Credit_Date__c, npsp__First_Soft_Credit_Amount__c, npsp__Exclude_from_Household_Name__c, npsp__Exclude_from_Household_Informal_Greeting__c, npsp__Exclude_from_Household_Formal_Greeting__c, npsp__Do_Not_Contact__c, npsp__Deceased__c, npsp__Current_Address__c, npsp__Batch__c, npsp__Address_Verification_Status__c,'+ 
                   'npo02__Total_Household_Gifts__c, npo02__TotalOppAmount__c, npo02__TotalMembershipOppAmount__c, npo02__SystemHouseholdProcessor__c, npo02__Soft_Credit_Two_Years_Ago__c, npo02__Soft_Credit_Total__c, npo02__Soft_Credit_This_Year__c, npo02__Soft_Credit_Last_Year__c, npo02__SmallestAmount__c, npo02__OppsClosedThisYear__c, npo02__OppsClosedLastYear__c, npo02__OppsClosedLastNDays__c, npo02__OppsClosed2YearsAgo__c, npo02__OppAmountThisYear__c, npo02__OppAmountThisYearHH__c, npo02__OppAmountLastYear__c, npo02__OppAmountLastYearHH__c, npo02__OppAmountLastNDays__c, npo02__OppAmount2YearsAgo__c, npo02__NumberOfMembershipOpps__c, npo02__NumberOfClosedOpps__c, npo02__Naming_Exclusions__c, npo02__MembershipJoinDate__c, npo02__MembershipEndDate__c, npo02__LastOppAmount__c, npo02__LastMembershipOrigin__c, npo02__LastMembershipLevel__c, '+
                   'npo02__LastMembershipDate__c, npo02__LastMembershipAmount__c, npo02__LastCloseDate__c, npo02__LastCloseDateHH__c, npo02__LargestAmount__c, npo02__Household__c, npo02__Household_Naming_Order__c, npo02__Formula_HouseholdPhone__c, npo02__Formula_HouseholdMailingAddress__c, npo02__FirstCloseDate__c, npo02__Best_Gift_Year__c, npo02__Best_Gift_Year_Total__c, npo02__AverageAmount__c, npe01__Work_Address__c, npe01__WorkPhone__c, npe01__WorkEmail__c, npe01__Type_of_Account__c, npe01__SystemAccountProcessor__c, npe01__Secondary_Address_Type__c, npe01__Private__c, npe01__Primary_Address_Type__c, npe01__Preferred_Email__c, npe01__PreferredPhone__c, npe01__Other_Address__c, npe01__Organization_Type__c, npe01__Home_Address__c, npe01__HomeEmail__c, '+
                   'npe01__AlternateEmail__c, Total_Donado_Hist_rico__c, Total_Donado_2014__c, Total_Donado_2013__c, Total_Donado_2012__c, Total_Donado_2011__c, Total_Donado_2010__c, Total_Donado_2009__c, Total_Donado_2008__c, Total_Donado_2007__c, Total_Donado_2006__c, Total_Donado_2005__c, Total_Donado_2004__c, Total_Donado_2003__c, Total_Donado_2002__c, Total_Donado_2001__c, Title, Tipo_de_registro_CANDIDATO__c, Tiene_nietos__c, Tiene_hijos__c, SystemModstamp, Salutation, ReportsToId, Referente__c, RecordTypeId, PhotoUrl, Phone, Pa_s_Aldeas__c, PSN__c, PSN_Historico__c, PSN_Buscar_Contacto__c, OwnerId, OtherStreet, OtherStateCode, OtherState, OtherPostalCode, OtherPhone, OtherLongitude, OtherLatitude, OtherCountryCode, OtherCountry, OtherCity, OtherAddress, Ocupaci_n__c, No_Recibir_Correo_F_sico__c, Ni_Apadrinado__c, Name, N_mero_de_Documento__c,'+
                   'MobilePhone, Media_2014__c, Media_2013__c, Media_2012__c, Media_2011__c, Media_2010__c, Media_2009__c, Media_2008__c, Media_2007__c, Media_2006__c, Media_2005__c, Media_2004__c, Media_2003__c, Media_2002__c, Media_2001__c, MasterRecordId, MailingStreet, MailingStateCode, MailingState, MailingPostalCode, MailingLongitude, MailingLatitude, MailingCountryCode, MailingCountry, MailingCity, MailingAddress, M_xima_2014__c, M_xima_2013__c, M_xima_2012__c, M_xima_2011__c, M_xima_2010__c, M_xima_2009__c, M_xima_2008__c, M_xima_2007__c, M_xima_2006__c, M_xima_2005__c, M_xima_2004__c, M_xima_2003__c, M_xima_2002__c, M_xima_2001__c, LeadSource, LastViewedDate, LastReferencedDate, LastName, LastModifiedDate, LastModifiedById, LastCUUpdateDate, LastCURequestDate, LastActivityDate, JigsawContactId, Jigsaw, IsEmailBounced, IsDeleted, Id, '+
                   'ID_Externo_Hist_rico__c, HomePhone, Hobbie__c, HasOptedOutOfFax, HasOptedOutOfEmail, Gender__c, FirstName, Fecha_nacimiento_CANDIDATO__c, Fecha_de_ltima_donaci_n_hist_rica__c, Fecha_de_Pr_ximo_Cumplea_os__c, Fecha_de_Captaci_n__c, Fecha_de_Captaci_n_Migraci_n__c, Fecha_de_Activaci_n_Unificada__c, Fecha_de_Activaci_n_Hist_rica__c, Fax, Estado_Civil__c, EmailBouncedReason, EmailBouncedDate, Email, Edad__c, Duplicado__c, DoNotCall, Destino_de_la_Donaci_n__c, Description, Department, Cuotas_pagadas_2009__c, Cuotas_Pagadas_2014__c, Cuotas_Pagadas_2013__c, Cuotas_Pagadas_2012__c, Cuotas_Pagadas_2011__c, Cuotas_Pagadas_2010__c, Cuotas_Pagadas_2008__c, Cuotas_Pagadas_2007__c, Cuotas_Pagadas_2006__c, Cuotas_Pagadas_2005__c, Cuotas_Pagadas_2004__c, Cuotas_Pagadas_2003__c, Cuotas_Pagadas_2002__c, Cuotas_Pagadas_2001__c, Cuotas_No_Pagadas_2014__c,'+ 
                   'Cuotas_No_Pagadas_2013__c, Cuotas_No_Pagadas_2012__c, Cuotas_No_Pagadas_2011__c, Cuotas_No_Pagadas_2010__c, Cuotas_No_Pagadas_2009__c, Cuotas_No_Pagadas_2008__c, Cuotas_No_Pagadas_2007__c, Cuotas_No_Pagadas_2006__c, Cuotas_No_Pagadas_2005__c, Cuotas_No_Pagadas_2004__c, Cuotas_No_Pagadas_2003__c, Cuotas_No_Pagadas_2002__c, Cuotas_No_Pagadas_2001__c, CreatedDate, CreatedById, Cantidad_de_donaciones_No_Pagadas__c, Cantidad_de_Hijos__c, Cantidad_cuotas_No_Pagadas_Hist_ricas__c, Cantidad_Cuotas_Pagadas_Hist_ricas__c, CallCenter_en_Contacto__c, CallCenter_Usuario_Contacto__c, CallCenter_Date_Contacto__c, Birthdate, AssistantPhone, AssistantName, AccountId FROM Contact'); 
    }
    
    global void execute(Database.BatchableContext BC, List<Contact> scope){
        i += scope.size();
        j ++;
        SendHandler sd = new SendHandler();
        try {
            sd.sendDonors(scope,1);
        } catch (Exception e){
            exceptions.add(e.getMessage());
        }
    }
    
    global void finish(Database.BatchableContext BC) {
        system.debug('#### list size: ' + i);
        system.debug('#### number of executes: ' + j);
        System.debug('#### Exceptions: ' + exceptions);
    }
}

but I am getting the same without the try/catch, also the log is ON, but I can't see any error.
MythreyeeSSMythreyeeSS
Hi Angello - With the same Start method (with no where condition) and same finish method, I would expect the values of the variables i and j should be same with the callout sd.sendDonors or without the callout. In the logic sd.sendDonors, if the code exists to delete contact or to update contact so that start method of query condition is not satisfied, then the i and j values may result in different values with and without callouts. 
Angello Camacho DragoAngello Camacho Drago
MythreyeeSS, in the sd.sendDonors method I only send the data to an external system through a callout, I don't make any DML statement.