+ Start a Discussion
Shishir VKShishir VK 

Not able insert data in Apex-Batch : "Finish()" .

Hi,


My main aim is to add some addition info to the data base after all the batchs are complete.. hence I need to invoke a database insert in the "final()" method of the Apex-batch job.


But I can see that the insert method is not called inside the finish() method. In fact i cannot even see the Syste.debug() messages added inside the finish() method.


Am sure that there is no exception been thrown inside the finish, as the status of the batch job is shown as completed (Also I have added an try catch block to doubl-check).


Please find below the sample code for your kind reference:


global class BatchAccountData implements Database.Batchable<SObject>{

// define a global variable:

List<My_CusObj__c> mylst = new List<MyCusObj__c>()

global Database.QueryLocator start(Database.BatchableContext bc) {
         Database.QueryLocator queryLocator = Database.getQueryLocator("select name,id from account");
         return queryLocator ;
   }

global void execute(Database.BatchableContext BC,
                              List<Account> accountListParam){

    for(Account acc: accountListParam ){
        MyCusObj__c obj = new MyCusObj__c();
      obj.Name = acc.Name+Bc.getJobId();
       mylst.add(obj);
    }
}

global void finish(Database.BatchableContext BC){
  insert mylst;    
}

}

Please provide me with your inputs, so that I can accomplish the aim.


Thanks in advance

bob_buzzardbob_buzzard

Are you sure that the list isn't empty?

garybgaryb

I think it is empty because the class does not implement Database.Stateful.

 

To the OP: Check out the section on this page called "Using State in Batch Apex": http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm