You need to sign in to do that
Don't have an account?
How to schedule batch apex
Hi Folks,
I have created a batch apex and i would like run it for every hour of every day
my btach apex class is as follows:
global class ReassignAccountOwner implements Database.Batchable<SObject>{
String query;
String email;
id fromUser;
id toUser;
global ReassignAccountOwner(String q,string e,Id fu,Id tu){
query=q;email=e;fromUser=fu;toUser=tu;
}
global Database.querylocator start(Database.BatchableContext bc){
//return Database.querylocator(query);
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext bc,List<Sobject> scope){
list<Account> accList = new list<Account>();
for(Sobject s:scope){
Account acc=(Account)s;
if(acc.ownerId==fromUser){
acc.ownerId=touser;
accList.add(acc);
}
}
update accList;
}
global void finish(Database.BatchableContext bc){
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
mail.setToAddresses(new String[] {email});
mail.setReplyTO('anyemail@anyemaill.com');
mail.setSubject('Batch completed successfully');
mail.setsenderdisplayName('SFDC Admin');
mail.setPlainTextBody('Batch Process Completed!!');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
My SChedular class:
global class RunBatch_ReassignAccountOwner implements Schedulable{
User toUser=[select id,name,email from user where username='abc@abcexample.com'];
User fromUser=[select id,name,email from user where username='def@defexample.com'];
String q='select id,ownerid from Account where ownerId=\''+fromUser.id+'\'';
String email='admin@adminexample.com';
global void execute(schedulablecontext sc){
ReassignAccountOwner reAssign = new ReassignAccountOwner (q,email,fromUser.id,touser.id);
ID batchprocessid = Database.executeBatch(reAssign);
}
}
Here i have added the batch class to the class 'RunBatch_ReassignAccountOwner ' that implements schedulable interface and here how to mention the time interval so batch class should run for every hour.
Thanks.
Check this link :
Apex: Scheduler
Hi Rahul,
Thx for your reply.
How can add System.schedule(....); in the class ''RunBatch_ReassignAccountOwner ' so it should run batch apex for every one hour.
I have gone through the link but i didnot understand the case with 'Batch Apex'.
Thanks.
For scheduling a batch there are two methods:
1. Through setup menu (going to Develop -> Apex Classes, click on the button 'Schedule Apex').
2. Run the code in system log containing schedule class object and the system.schedule method to schedule batch.
The code needed to schedule batch apex from system log is below:
Can you please share your code to test this scheduler class?
Thanks in Advance,
Hi,
you can run/test the schedule class 2 ways:
1. go to System log (Setup-->System log) and click on execute button(which is top left) and paste the below code to initiate the schedule class.
public class ScheduleBatchApex{
RunBatch_ReassignAccountOwner rrAcc= new RunBatch_ReassignAccountOwner();
String sch = '0 0 * * * ?';
//String sch = '20 30 8 10 2 ?';
system.schedule('SCheduled Job',sch,rrAcc);
}
2. write the above code in the new apex class and click on schedule apex button (setup-->App setup-->develop-->Apex classes) and select the class , date time .
Thanks.
Thanks for the reply. It helped me to resolve my question.