+ Start a Discussion
alok29novalok29nov 

Scheduler

Hi All,

 

I am trying to run a batsh class at regular intervals. I have writen below class but geting an error " expecting a right parentheses, found 'One'.

 

global class scheduledMerge10 implements Schedulable{

global void execute(SchedulableContext SC) {

AverageSpentDays b = new AverageSpentDays();

database.executebatch(b);
}
Schedulemerge10 b = new Schedulemerge10();
String sch = '0 0 7 07 12 ?';
system.schedule ('One', sch, b);

}

 

Is above the right way of writing a class for  scheduler?

 

~Alok

Best Answer chosen by Admin (Salesforce Developers) 
UVUV

Let me correct, to execute the batch job from the schedulable class sysntax would be like-

global class scheduledMerge implements Schedulable

{
global void execute(SchedulableContext SC)

{
BatchApexClass bac = new BatchApexClass() ;
Database.executeBatch(rap);
}
}

 

However, if you want schedule it from the apex programming then execute the code from the anonymous block in this way-

scheduledMerge m = new scheduledMerge();
String sch = '20 30 8 10 2 ?';
system.schedule('Merge Job', sch, m);

 

To schedule a batch job is one time need to just run the code from the anonymous block..

All Answers

UVUV

You should write in this way-

global class scheduledMerge10 implements Schedulable{

global void execute(SchedulableContext SC) {

AverageSpentDays b = new AverageSpentDays();


Schedulemerge10 b = new Schedulemerge10();
String sch = '0 0 7 07 12 ?';
system.schedule ('One', sch, b);

}

}

 


alok29novalok29nov

I am getting this error "Duplicate variable: b (attempt to re-create the variable with type: Schedulemerge10)" as vaqriable b is defined twice.

 

My batch class is AverageSpentDays. I tried some tweaking but still no luck.

 

How to make sure that system.scheduler runs batch class AverageSpentDays at specified time.

 

~alok

UVUV

Replace this

Schedulemerge10 b = new Schedulemerge10();

 

with

 

Schedulemerge10 sch = new Schedulemerge10();

alok29novalok29nov

If I replace that, how would I pass the value?

 

String sch = '0 0 7 07 12 ?'

UVUV

Let me correct, to execute the batch job from the schedulable class sysntax would be like-

global class scheduledMerge implements Schedulable

{
global void execute(SchedulableContext SC)

{
BatchApexClass bac = new BatchApexClass() ;
Database.executeBatch(rap);
}
}

 

However, if you want schedule it from the apex programming then execute the code from the anonymous block in this way-

scheduledMerge m = new scheduledMerge();
String sch = '20 30 8 10 2 ?';
system.schedule('Merge Job', sch, m);

 

To schedule a batch job is one time need to just run the code from the anonymous block..

This was selected as the best answer
UVUV

In addition to my last comment, in your case execute the below code from the anonymous block-

Schedulemerge10 b = new Schedulemerge10();
String sch = '0 0 7 07 12 ?';
system.schedule ('One', sch, b);

alok29novalok29nov

It is working fine. Just have one doubt.. If I want to run the batch class let say 10:10 pm, 10:20 pm, 10:25 pm then every time i have to run it from anonymous block as I have to check whether my code is giving the expected result.

I am aware that we can schedule a batch class from apex classes itself(schedule apex)  but that is hourly only.

Please correct me if I am wrong.

 

Thanks

Alok

UVUV

You can schedule a batch job on perticular interval..So, if you want  to schedule it after every 5 minutes then put that logic in execute from the anonymous class...

 

Note-There can be only 5 active jobs at a time.

KruviKruvi

Hi

 

I need advice on how I schedule a job in 5 min interval.

I was only able to do it hourly

 

Many thanks