[SalesForce] Batch Apex – Static List not available in finish method

The issue I am facing is that I have a global static list declared in the batchClass.

This list is populated after the values are obtained from a third party webservice.

I am not able to understand why the values vanish when they come into the finish method of the batch class. The variable has been declared as a static variable.

Please note that portions of the code that are irrelevant have been taken out.

global class batchIdentifyDupeLead implements Database.Batchable<sObject>, Database.Stateful, Database.AllowsCallouts 
{
Map<Id, Lead> checkLeads = new Map<Id, Lead>();    
global static List<Duplicate_Lead__c> dlc = new List<Duplicate_Lead__c>();    

global batchIdentifyDupeLead(Map<Id, Lead> myleads)    
{
  System.Debug('------------------------------------------------');
  System.Debug('fired constructor');
  System.Debug('------------------------------------------------');
    checkLeads = myleads;    
   // dlc = new List<Duplicate_Lead__c>();    
}
global final String Query;
global Database.QueryLocator start(Database.BatchableContext BC) 
{
    // Access initialState here 
    return Database.getQueryLocator([SELECT Id, Site_Zip__c, Email, Web_Source__c, LeadSource FROM Lead WHERE Id IN :checkLeads.keySet()]);
}

global void execute(Database.BatchableContext BC, List<Lead> batch) 
  {
    System.Debug('batch size executing is ... ' +  batch.size());
    for(Integer i=0;i<batch.size();i++)
    {
        System.Debug(batch.get(i).Site_Zip__c);
        if(batch.get(i).LeadSource == 'IN Internet' && batch.get(i).Web_Source__c != '')
        {
            callCloudingo(batch.get(i).Site_Zip__c, batch.get(i).Email, batch.get(i).Id);
        }
    }
        //Check for DLC contents DLC contents are available here.
             System.Debug('------------------------------------------------');
             System.Debug('DLC values (execute)--> '+ dlc);
             System.Debug('------------------------------------------------');

  }

  global void finish(Database.BatchableContext BC) 
  {
    // DLC values are no longer available.
             System.Debug('------------------------------------------------');
             System.Debug('DLC values (finish)--> '+ dlc);
             System.Debug('------------------------------------------------');

          if(dlc.size() > 1) {insert dlc;}
  }

public static void callCloudingo(string sitezip, string email, Id leadid)
    {

         HttpResponse response = http.send(request);  
             System.Debug('DLC Values are available here.');
             System.Debug('DLC values --> '+ dlc);
             System.Debug('------------------------------------------------');
                for(string s:returnList)
                {
                    dlc.add(new Duplicate_Lead__c(Primary_Lead__c = leadid, Duplicate_Lead__c = s));
                }
    }    

}

Best Answer

In batch jobs, static variables don't maintain state - they are re-set in each transaction. If you want to maintain state and access it in the finish method use member variables.

Here's the relevant info from the docs

If you specify Database.Stateful in the class definition, you can maintain state across these transactions. When using Database.Stateful, only instance member variables retain their values between transactions. Static member variables don’t retain their values and are reset between transactions. Maintaining state is useful for counting or summarizing records as they’re processed. For example, suppose your job processed opportunity records. You could define a method in execute to aggregate totals of the opportunity amounts as they were processed.

If you don’t specify Database.Stateful, all static and instance member variables are set back to their original values.

SF docs: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_batch_interface.htm

Related Topic