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.

  • 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 '14 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. – user3724260 Sep 26 '14 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 '14 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.... – user3724260 Sep 26 '14 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()? – user3724260 Sep 27 '14 at 00:05

1 Answers1

1

Here you go:

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

Another reference as well:

Cascading batch jobs

techbusinessman
  • 2,218
  • 19
  • 39
  • Man you are awesome! Will try that. – user3724260 Sep 26 '14 at 22:31
  • Please mark as the answer if it works :) – techbusinessman Sep 26 '14 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()? – user3724260 Sep 27 '14 at 00:07
  • @user3724260 - Are you using database.stateful? – Eric Sep 27 '14 at 00: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. – techbusinessman Sep 28 '14 at 01:07
  • Also as Eric has said you can use database.stateful to capture all the necessary id's. Keep in mind though that depending on the amount of data, you might run into the list record limit or heap space limits. – techbusinessman Sep 28 '14 at 01:53
  • Thanks! One last question: once I get all the data I need from the batch jobs, how do I pass those data back to a class? – user3724260 Sep 28 '14 at 20:19
  • Typically, you would save the ids you need to use in the next batch class to the database. Once you hit finish in the first batch, you call the second batch. The second batch then queries the database for the ids that you saved in the first batch and as part of it's execute method, requery for the ids and process the data. Hopefully that helps. – techbusinessman Sep 29 '14 at 20:51