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
fiona gentryfiona gentry 

How do i setup this batch job to execute at 6am PST Mon tuesday wed thursday

Dear gurus,

How do i setup this batch job to execute at 6am PST Mon tuesday wed thursday
 
global class CopyExecutive_RTBatch implements Database.Batchable<sObject> {
  
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
        
        String query = 'select Case__c, Level_1__c, Level_2__c,Level_3__c  FROM Executive_RT_Case_Type__c';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Executive_RT_Case_Type__c> exeList) {
       
        // process each batch of records
List<Case_Type__c> listCTD = new List<Case_Type__c>();
        
        for(Executive_RT_Case_Type__c exe : exeList)
        {        
           	listCTD.add(new Case_Type__c(Case__c=exe.Case__c,Level_1__c=exe.Level_1__c,Level_2__c=exe.Level_2__c,Level_3__c=exe.Level_3__c));
            //System.debug('Executive_RT Case No is =====>' +Case__c);
        }
        try {
        	
            insExecutive_RT listCTD;
        
        } catch(Exception e) {
            System.debug(e);
        }
        
    }   
    
    global void finish(Database.BatchableContext BC) {
    	// execute any post-processing operations
  }
}

Thanks,
Fiona
Best Answer chosen by fiona gentry
AbhishekAbhishek (Salesforce Developers) 
Yes, we can schedule batch by System.scheduleBatch method but to run a batch on a specific time you have to take the help of Scheduler. E.g

global class scheduledBatchable implements Schedulable {
  global void execute(SchedulableContext sc) {
  batchable b = new batchable(); // your batch class
  database.executebatch(b);
  }
}

You can set it to run via the point and click ui or with apex code like this:

scheduledBatchable b = new scheduledBatchable();
String sch = '20 30 8 10 2 ?'; // This is just like a regular cron job
system.schedule('Job Name', sch, b);

This blog is good to learn Cron expression.

http://sanjaal.com/java/558/cron-expressions/list-of-sample-cron-expressions-to-define-simple-and-complex-scheduling/

\\

For further reference, you can check this developer's discussion too (https://salesforce.stackexchange.com/questions/133871/cron-to-schedule-batch-class-to-run-everyday-at-10pm).

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.

All Answers

AbhishekAbhishek (Salesforce Developers) 
Yes, we can schedule batch by System.scheduleBatch method but to run a batch on a specific time you have to take the help of Scheduler. E.g

global class scheduledBatchable implements Schedulable {
  global void execute(SchedulableContext sc) {
  batchable b = new batchable(); // your batch class
  database.executebatch(b);
  }
}

You can set it to run via the point and click ui or with apex code like this:

scheduledBatchable b = new scheduledBatchable();
String sch = '20 30 8 10 2 ?'; // This is just like a regular cron job
system.schedule('Job Name', sch, b);

This blog is good to learn Cron expression.

http://sanjaal.com/java/558/cron-expressions/list-of-sample-cron-expressions-to-define-simple-and-complex-scheduling/

\\

For further reference, you can check this developer's discussion too (https://salesforce.stackexchange.com/questions/133871/cron-to-schedule-batch-class-to-run-everyday-at-10pm).

I hope you find the above information is helpful. If it does, please mark as Best Answer to help others too.

Thanks.
This was selected as the best answer
fiona gentryfiona gentry
Thanks could you write the below code in you above code sample ,Please help me in writing below class to the class you mentioned above
global class CopyExecutive_RTBatch implements Database.Batchable<sObject> {
  
    global Database.QueryLocator start(Database.BatchableContext BC) {
        // collect the batches of records or objects to be passed to execute
        
        String query = 'select Case__c, Level_1__c, Level_2__c,Level_3__c  FROM Executive_RT_Case_Type__c';
        return Database.getQueryLocator(query);
    }
    
    global void execute(Database.BatchableContext BC, List<Executive_RT_Case_Type__c> exeList) {
       
        // process each batch of records
List<Case_Type__c> listCTD = new List<Case_Type__c>();
        
        for(Executive_RT_Case_Type__c exe : exeList)
        {        
           	listCTD.add(new Case_Type__c(Case__c=exe.Case__c,Level_1__c=exe.Level_1__c,Level_2__c=exe.Level_2__c,Level_3__c=exe.Level_3__c));
            //System.debug('Executive_RT Case No is =====>' +Case__c);
        }
        try {
        	
            insExecutive_RT listCTD;
        
        } catch(Exception e) {
            System.debug(e);
        }
        
    }   
    
    global void finish(Database.BatchableContext BC) {
    	// execute any post-processing operations
  }
}



 
fiona gentryfiona gentry
Hi. i tried changing code but getting below error at line 15
 
Loop must iterate over collection: String





code is below

 
global class CopyERTBatchDaily Implements Schedulable
    {
        global void execute(SchedulableContext sc)
        {
            setERTBatch();
        }

        public void setERTBatch()
        {
            //List<Executive_RT> exeList = new List<Executive_RT>();
            String exeList = 'select Case__c, Level_1__c, Level_2__c,Level_3__c  FROM Executive_RT';
             // process each batch of records
            List<Case_Type__c> listCTD = new List<Case_Type__c>();
        
        for(Executive_RT exe : exeList)
        {        
               listCTD.add(new Case_Type__c(Case__c=exe.Case__c,Level_1__c=exe.Level_1__c,Level_2__c=exe.Level_2__c,Level_3__c=exe.Level_3__c));
            //System.debug('ERT Case No is =====>' +Case__c);
        }
        try {
            
            insert listCTD;
        
        } catch(Exception e) {
            System.debug(e);
        }
            
        }
    }