I have a scenario where users upload a file and a batch1 kicks off to create the records , once the records are created we pass on the ids we just created to another batch job (batch2) which is from a managed package and we do not have control over its finish method . The requirement to de dupe all these records once the 2nd job finishes and send an email to the user. What is the best way to trigger the batch3 ? The expectation is the users cannot wait for more than 10 mins for the process to finish.

Should I just schedule the batch3 at a certain time , and check the status of the previous batch and reschedule the batch3 if the batch2 has not finished? What is the best practice to schedule batches frequency wise? Please let me know if you need any clarification

1 Answer 1


You'll not be able to touch the code of the second batch, of course, because it's a managed class. That leaves you with really only two options.

The first is polling. Your code runs on a schedule - as Schedulable Apex - and runs Apex queries to determine whether the second batch has completed, successfully or not; once it observes completion, it kicks off the third batch class in your sequence. Without knowing more about the data operations performed by the second batch class, it's hard to be more specific. Here, you could for example pass the Schedulable class the Apex job Id of the second batch class, so you could actually take a look at its progress, and you could also supply the Schedulable class with information about the scope of records impacted, which could persist through to the third batch class.

The second approach is event-driven. You'd create a trigger on the record that's mutated by the second batch class, and have that trigger call a service class that similarly queries to determine whether or not your second batch has completed.

It's trickier to see with the second approach how you'd determine that a single discrete process had completed and take action. Perhaps you'd query AsyncApexJob based on the ApexClassID corresponding to your second batch class. But I think you'd need a second store of data about in-process operations (say a cache in some sObject or custom setting, initialized by your first batch class, about second-batch-class jobs that have been initiated) so that you can tell when a completion even has happened and collect any additional required details (like record scope) to pass to an invocation of your third batch class.

I'd probably take the former approach, and I think it's more likely to be the correct solution based on what information you've shared.. But this is highly fact-specific as far as the design goes, and you'll need to look hard at how you can persist job details across these several asynchronous processes to ensure that you're able to ensure continuity across the job, as well as error recovery and reporting.

  • Thank you David, trigger kicking off batch wont be an option , I will take the first approach and see Aug 12, 2019 at 14:58

You must log in to answer this question.

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