0

i want to call a batch apex from trigger when all the data load in that object is completed.But I am doing this I am getting the below error.

System.AsyncException: Database.executeBatch cannot be called fro...

It looks like we need to create a custom object and a checkbox field where we need to update that field on data load and then write a trigger on that object to ensure that the batch gets called once. Is there any other wayout?

3
  • looks like it going in recursion ? are you updating records from batch again
    – Ratan Paul
    Jan 11, 2018 at 13:17
  • @RatanPaul yes I am updating records through batch again....I tried to use boolean static variable in trigger but it is not working Jan 11, 2018 at 13:18
  • We can resolve the recursion stuff but my question is why are you calling the batch class from trigger. Calling future method or Queueable doesn't solve your use case? If yes then its advisable to go for them instead of calling batch class. Jan 11, 2018 at 13:33

1 Answer 1

0

looks like it going in recursion ? are you updating records from batch again

I would suggest first if the trigger is already from batch or not.

//check here is trigger is not fired from your batch then batch again execute

if(!System.isBatch()) 
{
  //execute your batch here
}

System.isBatch()
Returns true if a batch Apex job invoked the executing code, or false if not. In API version 35.0 and earlier, also returns true if a queueable Apex job invoked the code.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_system.htm

3
  • I have 12,000 records and for each record I am calling batch apex but it looks like only 5 batch jobs can run at a time. Jan 11, 2018 at 13:24
  • @DebarunSengupta why you are calling batch for each record. store all the records and together call the batch class in one go
    – Ratan Paul
    Jan 11, 2018 at 13:27
  • can we connect on this?Need help Jan 11, 2018 at 14:12

Not the answer you're looking for? Browse other questions tagged .