1

I want to know whether it is practically possible to pass a parameter from first batch apex into second batch apex. I do not want code but a brief concept would be greatly helpful.

3

Yes, you simply pass the values to the next batch, typically in the constructor.

public class Batch1 implements Database.Batchable<SObject> {
  Integer veryImportantValue;
  public Batch1(Integer someParam) {
    veryImportantValue = someParam;
  }
  // omitting start and execute
  public void finish(Database.BatchableContext context) {
    Database.executeBatch(new Batch2(veryImportantValue));
  }
}

The first batch would be called with the parameter:

Database.executeBatch(new Batch1(42));
9
  • thanks for your prompt reply. I have one doubt, do we need to sue database.stateful to maintain the state of across transactions? – Pooja Biswas Jan 14 '17 at 4:51
  • 4
    @PoojaBiswas Anything you set before start method runs will be available in the finish method without Database.Stateful. This will improve performance. You only need to use Database.Stateful if you need to modify the variables during the execute method. – sfdcfox Jan 14 '17 at 4:52
  • thanks, there's one more thing I would like to ask, its a separate topic. I hope u dont mind. what is Test.isRunningTest() method used for. how can I use this in batch apex and callout? – Pooja Biswas Jan 14 '17 at 4:57
  • @PoojaBiswas You shouldn't use Test.isRunningTest for callouts usually. You should be able to test it with just Test.setMock. – sfdcfox Jan 14 '17 at 4:58
  • can we use Test.isRunningTes() to check of first batch has completed and then start the second ? – Pooja Biswas Jan 14 '17 at 5:01

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.