2

I have a scenario where I insert orderItem records using a batch class. I have an after insert trigger on OrderItem that calls a batch to update on other object.

I'm getting an error

"System.AsyncException: Database.executeBatch cannot be called from a batch start, batch ..."

when after insert trigger fires.

It doesnt give me an error when i put this in trigger code,

if(trigger.new !=null && trigger.new.size() > 0 &&!system.isFuture() && !system.isbatch()){
        Database.executeBatch(new UpdateSubProductsBatch(trigger.new),500);
    }

but doesn't execute the batch from trigger.

If I take off the !system.isbatch() from if conditions, its gives me the above error.

Can you please help me in understanding the situation and how to deal with it.

I am using the batch after the after insert trigger, because i have to update bulk records.

6
  • why you need batch can't you used future method? – Ratan Paul Feb 17 '16 at 17:20
  • Will I not face the same error "System.AsyncException: Database.executeBatch cannot be called from a batch start, batch ...", when i use future, i mean execute a future method from after insert trigger? – Anurag Feb 17 '16 at 17:27
  • How you are inserting orderItem ? from batch i think? – Ratan Paul Feb 17 '16 at 17:28
  • 1
    Anurag... then dont execute UpdateSubProductsBatch batch from trigger.. instead when your first batch completed, call UpdateSubProductsBatch method from first batch's finish method. – Ratan Paul Feb 17 '16 at 17:31
  • 1
    Ya,i'll try that. – Anurag Feb 17 '16 at 17:34
1

Answer to your question

Dont execute UpdateSubProductsBatch batch from trigger..

instead when your first batch process completed, call UpdateSubProductsBatch method from first batch's finish method


You are trying to execute batch from inside a batch process that's the reason you are getting Error

System.AsyncException: Database.executeBatch cannot be called from a batch start, batch .


Updates

Use Database.Stateful to collect all Account.. And check in finish method..

global class SummarizeAccountTotal implements 
    Database.Batchable<sObject>, Database.Stateful{


   private List<Account> lstAccount = new List<Account>();


   global Database.QueryLocator start(Database.BatchableContext BC){
      return Database.getQueryLocator([SELECT ID from Account]);
   }

   global void execute(
                Database.BatchableContext BC, 
                List<sObject> scope){
      for(Account acc : scope){
         lstAccount.add(acc);
      }
   }

global void finish(Database.BatchableContext BC){
       system.debug('-----lstAccount-----'+lstAccount);
   }
}
4
  • Ratan, how do I pass a list of records that are inserted in execute method to finish, i need to pass them for the second batch. – Anurag Feb 17 '16 at 18:31
  • @AnuragA, You can use database.stateful or if you have too much data to pass then you can write the data to a custom object and reference that in the next batch. There are various options. – javanoob Feb 17 '16 at 21:34
  • @AnuragA yes use database.stateful – Ratan Paul Feb 18 '16 at 2:20
  • @AnuragA check my updated ans. you can follow like that and don't forgot to implement database.stateful – Ratan Paul Feb 18 '16 at 2:50

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.