1

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));
                }
    }    

}
5

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

2
  • I have specified Database.Stateful in my class declaration. – abhi Jun 5 '19 at 18:53
  • 2
    Right. you need to indicate that to maintain state with MEMBER variables. STATIC variables don't maintain state in batch apex (note without Database.Stateful your batch job wouldn't maintain state at all). I called out the section above from the SF documentation which discusses this. – PartOfTheOhana Jun 5 '19 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.