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
VSK98VSK98 

Could any body tell how write schedule class

Hi all,

 I need to run the schedule class for every day @ 1 PM ...........

Here is the my batch class

global class  sendwagenoticestoemployees Implements Database.Batchable <sObject> {
    global Database.queryLocator start(Database.BatchableContext bc) {
        
        String SOQL = 'SELECT Id, WTPAA_Employee_Email__c,WTPAA_Related_To__c,WTPAA_Living_Wage__c FROM WTPAA_Wage_Notice__c WHERE WTPAA_Living_Wage__c =  false ';
        return Database.getQueryLocator(SOQL);
    }

    global void execute(Database.BatchableContext bc, List<WTPAA_Wage_Notice__c> wagenotice) {
        List<Messaging.SingleEmailMessage> mailList = new List<Messaging.SingleEmailMessage>();
       Emailtemplate et = [select id, developername , IsActive from Emailtemplate where developername = 'WTC_Offer_is_Ready_for_Review' AND IsActive = true];
        for(WTPAA_Wage_Notice__c w: wagenotice) {
            List<String> toAddresses = new List<String>{w.WTPAA_Employee_Email__c};           
            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setToAddresses(toAddresses);
          
            mail.SetTemplateid(et.id);
            mail.setSaveAsActivity(false);
          
           mail.setTargetObjectId(w.WTPAA_Related_To__r.id);
           system.debug('ID of Contact' +w.WTPAA_Related_To__r.id);
          
            system.debug('IDDDDDDDDDDD*******' +et.id);
            mailList.add(mail);   
            system.debug('size of the list' +mailList.size() );    
        } 
        Messaging.sendEmail(mailList);        
    }

    global void finish(Database.BatchableContext bc) {
    }
}

 
please help me out.
 
Ketankumar PatelKetankumar Patel
global class sendwagenoticestoemployeesSchedule implements Schedulable {
    global void execute(SchedulableContext sc) {
        
         //Define your batch size.        
         integer BATCH_SIZE = 50;  
         sendwagenoticestoemployees pwnBatch = new sendwagenoticestoemployees ();
         system.debug('**** sendwagenoticestoemployeesSchedule : starting batch exection*****************');
         Id batchId = database.executeBatch(pwnBatch , BATCH_SIZE);    
        system.debug('**** sendwagenoticestoemployeesSchedule : Batch executed batchId: ' +batchId);
    }
}

Here is the schedule class for your batch class. You can even execute your batch class in exceute anonymous window from developer console too. 
 
integer BATCH_SIZE = 50;  
sendwagenoticestoemployees pwnBatch = new sendwagenoticestoemployees ();
system.debug('**** sendwagenoticestoemployeesSchedule : starting batch exection*****************');
Id batchId = database.executeBatch(pwnBatch , BATCH_SIZE);    
system.debug('**** sendwagenoticestoemployeesSchedule : Batch executed batchId: ' +batchId);


 
VSK98VSK98
Hi,
i have some doubts????????
can i use batch is 200;
I need to run the schedule class for every day @ 1 PM ..........
i have checked ur code there is no cron expression????????.
 
Ketankumar PatelKetankumar Patel
Then you can Schedule your schedulable class using standard apex schedulable interface. 

User-added image


User-added image
VSK98VSK98
Hi,

Shall i change the batch size.......??????

If i give morethan 200 what will happen to my schedule class? could u explain it ????


global class sendwagenoticestoemployeesSchedule implements Schedulable {

     global void execute(SchedulableContext sc) {

         

       //Define your batch size.       

       integer BATCH_SIZE = 100; 

      sendwagenoticestoemployees pwnBatch = new sendwagenoticestoemployees ();
     system.debug('**** sendwagenoticestoemployeesSchedule : starting batch exection*****************');

     Id batchId = database.executeBatch(pwnBatch , BATCH_SIZE);   

  system.debug('**** sendwagenoticestoemployeesSchedule : Batch executed batchId: '+batchId);

   }
 }
 
Ketankumar PatelKetankumar Patel
I hope this would help you. 

You can have batch size less or equals to 200. salesforce chunks the records returned by the Database.QueryLocator into batches of 200. 

I can see you are using your batch to send emails. There is a limit to send number of emails from salesforce.The daily limit for emails sent through email alerts is 1,000 per standard Salesforce license per organization—except for free Developer Edition and trial organizations, where the daily workflow email limit is 15 per standard Salesforce license. The overall organization limit is 2,000,000. This limit applies to emails sent through email alerts in workflow rules, approval processes, flows, processes, or the REST API.


Cron expression job to run everyday at 1 PM is following. use this to schedule your apex class in exceute anonymous window from developer console.
sendwagenoticestoemployeesSchedule p = new sendwagenoticestoemployeesSchedule();
String sch = '0 0 13 * * ?';
system.schedule('sendwagenoticestoemployeesSchedule M-F 1PM', sch, p);


 
VSK98VSK98
Thank you very much
Peter Friberg 26Peter Friberg 26
To save some classes I usually implement both "Schedulable" and "Batchable" interface in my scheduleable batch jobs :)
Then you can have the crontab definition in that as well making it easier to start it from the "Execute anonymous window".
Good luck!