function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
vespavespa 

Triggering One Batch Job from Another

Hi,

 

I have 3 apex batch jobs.

I run them in sequence. Lets call them BJ1, BJ2 and BJ3.

What I have noticed is that their execution time is very different. It seems to be dependent on,

 

1) the data in SF DB,

2) server congestion 

 

Yesterday it took me 4.5 hrs to run each job, which was ridiculous and in real scenario I need a solution to run them in sequence, so that I make sure that once BJ1 gets finished, then and only then BJ2 starts and once BJ2 gets finished BJ3 starts. And I also need to run them MORE THAN ONCE A DAY.

The interface jobs sceduler scedules the job once a day or I have to create multiple schedulers, which will be tedius as i'll need to schedule then every hour.

 

I need some help guys!!!! :smileysad:

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

Are you trying to start the next batch job directly from the previous one?  I'm not sure you'll be able to do that - I'd suggest you use the cron syntax to schedule the next job for as soon as it will allow you to.  It won't be instant though.

All Answers

bob_buzzardbob_buzzard

If you use scheduled apex, you can programmatically schedule the first run of BJ1, and have its final action be to initiate or schedule B2.  BJ2 does the same for BJ3, and that reschedules BJ1 to run however many hours later you require.

 

There's more information on programmatically scheduling apex at:

 

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

yashagarwalyashagarwal

if the size of the data is very large then it can take a lot of time for teh batch to execute. On an average a btach class takes about 5 minutes to complete execution.

 

Now , in order to execute them one after the other this is somethign you could try :

 

write the code for scheduling the second batch in the finish method of the first batch . Subsequetly , write teh code the code fro scheduling the third batch in the finish method for the second class.

 

Not sure , if that is what to acheive , but let me know if that helps.

vespavespa

Thanks bob,

 

 

I tried the same with the finish method, but got a message saying 'cann't initiate ....... something something'.

The link is for Apex Jobs basics and does not tell anything about running another job  from one job.

I am in desperate need of a solution as my projuect delivery date is nearing.

 

bob_buzzardbob_buzzard

Are you trying to start the next batch job directly from the previous one?  I'm not sure you'll be able to do that - I'd suggest you use the cron syntax to schedule the next job for as soon as it will allow you to.  It won't be instant though.

This was selected as the best answer
Hemant PatelHemant Patel

Hi,

 

In order to do this, send one email when first batch completed.

Create one Email Handler service which receive that email and parse and execute your second batch.

And do the same process for as many batches you want to execute.

 

Regards,

Hemant Patel

Salesforce.com Developer

vespavespa

Ok sounds good...

I guess I need to read more on this....

Thanks...

vespavespa

Alright it sounds very promissing ....

I am getting so many suggestions, so quickly ...

I've got stuff to read on cron for timing and email handler for seqencing...

If I pull this off, I'll be like a knight with shining armour here....

 

Thanks guys ....

vespavespa

Thanks hemant ..I am going to try this and let you know how it worked...

 

Gunners_23Gunners_23

hi bob,

 

I have done something similar. Inside finish method of 1st job i'm calling the 2nd batch scheduler. But when i'm running the test class for the first batch it doesn't touch the second batch. Even in the debug logs i coudn't find whether the 2nd job ran or not

 

Am i missing anything?

 

Thanks in advance