1

I'm trying to create an Iterable Batch class for inserting records. I'm able to insert my records in my normal class. I have created the Batch class and am trying to call it my current class in the following manner:

   BatchReportsInsertion b = new BatchReportsInsertion(reportsToAdd);

   Database.executeBatch(b);

My Batch class is as follows:

global class BatchReportsInsertion implements Database.Batchable<Report__c> 
{

List<Report__c> batchList;



global BatchReportsInsertion (List<Report__c> reportList) 
{

  this.batchList = reportList;
  System.debug('SIZEEE'+reportList.size()); // I can see this this in logs
  System.debug('Constructor is working!!'); // I can see this this in logs
}

global List<Report__c> start(Database.BatchableContext BC)
{ 
   System.debug('Is it returning the BatchList?!'); // I CANT see this in logs
   return this.batchList; 

}     

global void execute(Database.BatchableContext BC, List<Report__c> scope)
{
    insert scope;
    System.debug('Executing insert!!'); // I CANT see this in logs
}     

global void finish(Database.BatchableContext BC)
{     
} 

}

I'm not able to insert records using this. Please let me know what needs to be done in order to insert records using Batch class.

Thanks in advance :)

  • What volume of Report__c objects are you passing in? Do you see any BatchSerialChunkHandler type logs in your Developer Console when you do this? What do those logs say, or the Apex Jobs list in Setup? – David Reed Jun 30 '18 at 22:11
  • Hello @DavidReed, currently the reportList.size() is just 25. Also, I don't see any BatchSerialChunkHandler type logs. – Sneha Jagtap Jun 30 '18 at 22:23
  • 2
    When you look at the Apex Jobs list in Setup, do you see this batch job and what is its status? If you invoke it from Anonymous Apex, what log entries appear? One possibility is that Report__c includes non-serializable member variables. – David Reed Jun 30 '18 at 22:28
  • @DavidReed I don't see any error in the log entries. Also, I'm able to insert the same Report__c list with my current class. Hence I was wondering if something is wrong with my Batch class or the way I'm calling the Database.executeBatch(b); in the current class. Please let me know if there are any suggestions :) – Sneha Jagtap Jun 30 '18 at 22:45
  • 2
    Make sure you're checking all the logs; there should be four in total: however you called the method, one for the start method, one for the execute method, and one for the finish method. – sfdcfox Jul 1 '18 at 0:09
0

It looks like you have a batch class with a state, but without the right interface. You need to add the Database.Stateful interface to your class. It doesn't require any additional methods, but lets the system running the code know that you have additional variables in your class, which need to be maintained between each run. As soon as you hit start, your variable will be empty, and nothing with happen (maybe a null pointer exception).

Your example is pretty much a perfect example of when to use the interface. Basic rule of thumb is if you're using a constructor, you'll need to set the interface to keep any variables set inside it between runs. If your code is just passing records from the start method, and has no class variables, you're probably okay. See the documentation (about halfway down the page) for more info.

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.