+ Start a Discussion
Vamsee KaramchetiVamsee Karamcheti 

How can I schedule a job in salesforce twice a day ?

Hi Friends,

Can you please explain me How can I stop existing job which is running in my environment and How Can I schedule new job which runs twice a day ?
Best Answer chosen by Vamsee Karamcheti
Akshay_DhimanAkshay_Dhiman
Hi Vamsee,
Try this,
I assume you have created a Schedulable class. You can schedule it using the system.schedule command:
Step 1. In developer Console select debug menu and in that menu Open execute Anonymous Window
Step 2.
System.schedule('Scheduled Job 1', '0 0 11/12 * * ?', new yourclassName());
Hope this will help you. Please mark the answer as a solution.

Regards,
Akshay

All Answers

Akshay_DhimanAkshay_Dhiman
Hi Vamsee,
To Stop Scheduled job :
Navigate to Setup => (In Quick Find box) Scheduled Jobs
Now you have the list of scheduled jobs. Delete those which you don't want to execute.
To schedule new job which runs twice a day 
String sch = '0 0 11/12 * * ?';
//0 0 {time to schedule}/{hours until repeat} * * ?
Example To run twice a day, i.e 8 AM and 8 PM
String cron = '0 00 08,20 * * ?';
//Runs at 0800h and at 2000h every day
I hope it will help you.

Regards,
Akshay
Vamsee KaramchetiVamsee Karamcheti
Hi Akshay,

Thanks for your reply. I have one more doubt Where should I use below line of code ? In Batch class ?

String sch = '0 0 11/12 * * ?';
 
Akshay_DhimanAkshay_Dhiman
Hi Vamsee,
Try this,
I assume you have created a Schedulable class. You can schedule it using the system.schedule command:
Step 1. In developer Console select debug menu and in that menu Open execute Anonymous Window
Step 2.
System.schedule('Scheduled Job 1', '0 0 11/12 * * ?', new yourclassName());
Hope this will help you. Please mark the answer as a solution.

Regards,
Akshay
This was selected as the best answer
Harshit Garg 6Harshit Garg 6
HI Vamsee,

Please use this in the developer console Anonymous window. You will use something like below code.

For the running your Schedule class. Please fill your Schedule class name in the place of  Schedule_CreateNewRecords .
Schedule_CreateNewRecords  batchSch=new Schedule_CreateNewRecords ();
String sch='0 1 0 * * ?';
//System.schedule(String jobName, String cronExp, APEX_OBJECT schedulable);
System.schedule('Batch Schedule', sch , batchSch);

Till if you have not created schedule class.I have attached the code below.

Schedule class name- In the place of batchclassname. please fill your Batch class name. 
global class Schedule_CreateNewRecords implements Schedulable{
    global static void execute(SchedulableContext SC){
        //this simply runs the batch from step 2
        Batchclassname CNR = new Batchclassname();
        ID idBatch = Database.executeBatch(CNR); 
    }
}
Please mark my answer as a solution if it was helpful so it is available to others as a proper solution.
If you felt I went above and beyond, please give me Kudos by clicking on the star icon.

Thanks,
Harshit Garg

 
Harshit Garg 6Harshit Garg 6
HI,
 
Schedule_CreateNewRecords  batchSch=new Schedule_CreateNewRecords ();
String sch='0 0 11,12 *  *?';
//System.schedule(String jobName, String cronExp, APEX_OBJECT schedulable);
System.schedule('Batch Schedule', sch , batchSch);

Please use this in developer console Anonymous window.

Thanks,
Harshit Garg