You need to sign in to do that
Don't have an account?
Maintaining State across Batch job
Hello Ohana,
If I understood correctly, if I pass 1000 records in the below batch, it will divide across 5 sub batch(200 each). The List<CSVGenerator.Values> will hold value only for the final batch. What I am looking at is the List<CSVGenerator.Values> should store the values of all the 5 sub batches which I will be passing to another method in my FINISH method. Need help here.
If I understood correctly, if I pass 1000 records in the below batch, it will divide across 5 sub batch(200 each). The List<CSVGenerator.Values> will hold value only for the final batch. What I am looking at is the List<CSVGenerator.Values> should store the values of all the 5 sub batches which I will be passing to another method in my FINISH method. Need help here.
public class BatchToGenerateCSVForMarin implements Database.Batchable<sObject>, Database.Stateful { public List<CSVGenerator.Values> indiaGoogleValList = new List<CSVGenerator.Values>(); public List<CSVGenerator.Values> indiaFacebookValList = new List<CSVGenerator.Values>(); public Database.QueryLocator start(Database.BatchableContext BC){ String query = 'SELECT ID,Email,Status,CreatedDate,Lead_GEO__c,GCLID__c,MKWID__c,MSCLICKID__c,FBCLID__c FROM Lead WHERE RecordType.Name=\'B2C\' AND Status!=\'Duplicate\''; return Database.getQueryLocator(query); } public void execute(Database.BatchableContext BC, List<Lead>scope){ try{ Set<Id> indiaLeadID = new Set<Id>(); Set<Id> ROWLeadID = new Set<Id>(); Set<Id> USLeadID = new Set<Id>(); for(Lead l : scope){ if(l.Lead_GEO__c!=null && l.Lead_GEO__c =='INDIA'){ indiaLeadID.add(l.Id); } } Map<String,Integer> googleIDIndiaMap = new Map<String,Integer>(); Map<String,Integer> marinIDIndiaMap = new Map<String,Integer>(); List<AggregateResult> leadMarketingIDAgg = [SELECT Count(ID)id, GCLID__c googleID, MKWID__c marinID, MSCLICKID__c bingID, FBCLID__c facebookID FROM Lead WHERE ID IN:indiaLeadID Group By GCLID__c,MKWID__c,MSCLICKID__c,FBCLID__c]; for(AggregateResult agg : leadMarketingIDAgg){ if((String)agg.get('googleID')!=null){ googleIDIndiaMap.put((String)agg.get('googleID'), (Integer)agg.get('id')); } if((String)agg.get('marinID')!=null){ marinIDIndiaMap.put((String)agg.get('marinID'), (Integer)agg.get('id')); } } for(String s : googleIDIndiaMap.keySet()){ CSVGenerator.Values val = new CSVGenerator.Values(); val.recordDate = String.valueof(System.today()); val.conversionType = 'Lead'; val.conversions = string.valueof(googleIDIndiaMap.get(s)); val.revenue = '0'; val.currencyCode = ''; val.comments = s; indiaGoogleValList.add(val); } for(String s : marinIDIndiaMap.keySet()){ CSVGenerator.Values val = new CSVGenerator.Values(); val.recordDate = String.valueof(System.today()); val.conversionType = 'Lead'; val.conversions = string.valueof(marinIDIndiaMap.get(s)); val.revenue = '0'; val.currencyCode = ''; val.comments = s; indiaMarinValList.add(val); } } public void finish(Database.BatchableContext BC){ if(indiaGoogleValList.size() > 0){ CSVGenerator.generateCsv('Marin', indiaGoogleValList, 'Google', 'INDIA'); } if(indiaMarinValList.size() > 0){ CSVGenerator.generateCsv('Marin', indiaMarinValList, 'Marin', 'INDIA'); } } }
I see that you have already implemented Database.Stateful and that should do the trick to maintain state across batches. I have done some minor tweak in your code, please see if this helps.