1

Right now I need to schedule a batch call after a existing batch call finishes because I'm relying on the data from the first batch call in the second batch call. Is there a way to call scheduleBatch() after a batch is completed?

Edit: To clarify: I'm pulling a child-parent pair from an object, populating a map with those information, and from that map find the root of each child. Then in the second batch, I will insert the child, parent, root as a single record inside a new Object. We are also not allowed to use external ID so upserting is another challenge. Any help would be appreciated.

5
  • What do you mean by "relying on the data in the first batch call in the second batch call?" Can you say more about how you'd like to use that data?
    – greenstork
    Sep 26, 2014 at 21:44
  • Yes. Right now I'm pulling a parent-child pair from an Object. From those data, I need to find the root of each record. So the first batch would construct a map of the parent-child data. The second batch would then use the populated map to find the root of each record, and insert the id, parentID, and rootID as one record inside a new Object. Sep 26, 2014 at 22:21
  • Using Database.Stateful you could collect the data from the first batch, which you could then use in the finish method to call a second batch, but I don't believe that you could use the stateful data from the first batch in the execute method of the second batch. You would likely need to persist it in some temporary table or if you could get away with just having it for your cursor query for the second batch, that would work too.
    – greenstork
    Sep 26, 2014 at 22:35
  • That's my approach right now. But can you elaborate on how to make the temporary map and pass it onto the next batch? Right now I made a global map variable in the first map and passed it to the second batch class through a parameter, but that doesn't seem to be working as nothing is getting populated after the map is passed over.... Sep 26, 2014 at 23:26
  • My main issue right now is, after populating the map successfully in the execute() method in the first batch, I tried printing out the keyset in the finish() method, but nothing is getting populated. How do i pass the data from execute() to finish()? Sep 27, 2014 at 0:05

1 Answer 1

1

Here you go:

global void finish(Database.BatchableContext BC){
  Database.executeBatch(new MyBatch());
}

Another reference as well:

Cascading batch jobs

8
  • Man you are awesome! Will try that. Sep 26, 2014 at 22:31
  • Please mark as the answer if it works :) Sep 26, 2014 at 23:46
  • My main issue right now is, after populating the map successfully in the execute() method in the first batch, I tried printing out the keyset in the finish() method, but nothing is getting populated. How do i pass the data from execute() to finish()? Sep 27, 2014 at 0:07
  • @user3724260 - Are you using database.stateful?
    – Eric
    Sep 27, 2014 at 0:39
  • You have to store the result data in the database and then request in the second batch. Finish only occurs after all batches have processed. Sep 28, 2014 at 1:07

You must log in to answer this question.

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