You need to sign in to do that
Don't have an account?
Chris987654321
Help with Batch Apex code
global BatchUpdateAdmissionsGoals(String q, String e, String f, String v){ Query=q; Entity=e; Field=f;Value=v; opportunityList = new List <Opportunity> (); global Database.QueryLocator start(Database.BatchableContext BC){ return Database.getQueryLocator(query); } global void execute(Database.BatchableContext BC, List<sObject> scope){ //List <Admissions_Goal__c> goalsToUpdate = new List<Admissions_Goal__c>(); Integer totalLeads, weeklyLeads; for(sObject s : scope){ if(s.getsObjectType() == Opportunity.sObjectType) { Opportunity o = (Opportunity)s; opportunityList.add(o); } } System.debug('*** Opp list size ' + opportunityList.size()); } global void finish(Database.BatchableContext BC){ System.debug('*** Opp list size ' + opportunityList.size()); }
I am trying to write some batch Apex. When the following anonymous code block
string query = 'SELECT Id, OwnerID, Academic_Start_Date__c, Date_Booked__c ' + 'FROM Opportunity WHERE ID=\'006S0000003imhy\''; BatchUpdateAdmissionsGoals updateAdmissionsGoalsJob = new BatchUpdateAdmissionsGoals (query, '', '', ''); ID admissionsGoalsBatchJobID = Database.executeBatch(updateAdmissionsGoalsJob);
:
When I call the the debug statement that gets the opportunityList.size() the first time and I get 1 as expected. But when I call it in the Finish() method again, I get 0. It doesn't seem to keep the the items in opportunityList when it gets to the Finish() method
You need to implement the stateful interface to maintain your list. See the section, Using State in Batch Apex at the following URL:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm
Note the following line from the documentation: "If you do not specify Database.Stateful, all member variables in the interface methods are set back to their original values."
However, I would not recommend using the approach of maintaining a list of opportunities in your batch class. You are likely to hit the heap limit very quickly.
All Answers
You need to implement the stateful interface to maintain your list. See the section, Using State in Batch Apex at the following URL:
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm
Note the following line from the documentation: "If you do not specify Database.Stateful, all member variables in the interface methods are set back to their original values."
However, I would not recommend using the approach of maintaining a list of opportunities in your batch class. You are likely to hit the heap limit very quickly.
I see. Thank you. I think I will find another way to solve my problem but this is helpful for future reference.