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
CBNCBN 

How to Write a Scheduler class for Daily and weekly

Hi All,

How to write a scheduler class for daily and weekly for below Batch Apex
  • IF the recent Recharge record for 'Internet Plan' record was made 1 day ago and its 'Data Plan' = 'Daily', then create Recharge record.
  • IF the recent  Recharge for 'Internet Plan' was made 6 days ago and 'Data Plan' = 'Weekly', then create Recharge record.
    global class batchCreateRecharge implements Database.Batchable<sobject> {
      
        global Database.QueryLocator start(Database.BatchableContext bc){
          
            String query = 'SELECT Id, Data_Plan__c,Total_Amount__c FROM Internet_Plan__c';
            return Database.getQueryLocator(query);
        }
          
        global void execute(Database.BatchableContext bc, List<Internet_Plan__c> scope) {
          try {
         List<Recharge__c> rcList = new List<Recharge__c>();
            for(Internet_Plan__c IP : scope) {
               Recharge__c rc = new Recharge__c();
              rc.Amount__c = IP.Total_Amount__c;
               rc.Internet_Plan__c= IP.id;
             rcList.add(rc);
             System.debug('******Recharge list'+rcList);
            }
       insert rcList;
     }
             
                catch( DmlException e ) {
                    // HANDLES EXCEPTIONS RELATED TO DML STATEMENTS.
                    System.debug( 'QueryException:-\n' + e.getMessage() + '\nLine Number:-\n' + e.getLineNumber() );
                }
                catch( Exception e ) {
                    // HANDLES EXCEPTIONS OTHER THAN ABOVE SUCH AS NULL POINTER EXCEPTIONS OR ETC.
                    System.debug( 'QueryException:-\n' + e.getMessage() + '\nLine Number:-\n' + e.getLineNumber() );
                }
    
        } 
          
        global void finish(Database.BatchableContext bc) {
          
        }
    }

    Kindly Support and Suggest
Thanks
SFDC Apex DevSFDC Apex Dev
Hi CBN,

Try out the below code:

global class ScheduleCreateRecharge implements Schedulable{
 
  global void execute(SchedulableContext sc) {  
         batchCreateRecharge batch = new batchCreateRecharge ();
         Database.executeBatch(batch, 100); //max no of records need to process
  } 

then schedule your batch job from developer console-->debug-->anonymous window

system.schedule('ScheduleCreateRecharge ','0 0 22 1/1 * ? *', new ScheduleCreateRecharge ()); // you can check the cron(0 0 22 1/1 * ? *) for weekly and daily from this site : http://www.cronmaker.com/

Help Topics
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_batch_interface.htm
http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_scheduler.htm

Hope this will help you.

Regards,
Chirag
ManojjenaManojjena
Hi CBN,

You can write a scheduler calss like below ,
 
global class batchCreateRechargeSchedule implements Schedulable{
   global void execute(SchedulableContext sc) {
      //Instanciate the batch class 
      batchCreateRecharge  batchCreatRecha = new batchCreateRecharge (); 
      //executing the batch class 
      database.executebatch(batchCreatRecha);
   }
}

After that you can schedule the class with standard UI .

Setup>> Apex Classes > Schedule Apex (Button ) then below UI you can choose the class Name and contnue .

User-added image

Let me know incase it helps !!!
Thanks 
Manoj
Deepali KulshresthaDeepali Kulshrestha
Hi CBN,

You can schedule your batch class simply by using the cron trigger expression which will be fire according to your requirement. you can generate a cron trigger according to time,  day, week, month, etc. Please refer to the following code as they may be helpful in solving your query.

global class batchCreateRecharge implements Database.Batchable<sobject>,Schedulable {
  
    global Database.QueryLocator start(Database.BatchableContext bc){
      
        String query = 'SELECT Id, Data_Plan__c,Total_Amount__c FROM Internet_Plan__c';
        return Database.getQueryLocator(query);
    }
      
    global void execute(Database.BatchableContext bc, List<Internet_Plan__c> scope) {
      try {
     List<Recharge__c> rcList = new List<Recharge__c>();
        for(Internet_Plan__c IP : scope) {
           Recharge__c rc = new Recharge__c();
          rc.Amount__c = IP.Total_Amount__c;
           rc.Internet_Plan__c= IP.id;
         rcList.add(rc);
         System.debug('******Recharge list'+rcList);
        }
   insert rcList;
 }
         
            catch( DmlException e ) {
                // HANDLES EXCEPTIONS RELATED TO DML STATEMENTS.
                System.debug( 'QueryException:-\n' + e.getMessage() + '\nLine Number:-\n' + e.getLineNumber() );
            }
            catch( Exception e ) {
                // HANDLES EXCEPTIONS OTHER THAN ABOVE SUCH AS NULL POINTER EXCEPTIONS OR ETC.
                System.debug( 'QueryException:-\n' + e.getMessage() + '\nLine Number:-\n' + e.getLineNumber() );
            }

    } 
      
    global void finish(Database.BatchableContext bc) {
      
    }
}
  global void execute(SchedulableContext sc) {  
         batchCreateRecharge batch = new batchCreateRecharge ();
         Database.executeBatch(batch, 100); //max no of records need to process
  } 

--------- Run this in Excute Anonymous window----------------
system.schedule('ScheduleCreateRecharge ','0 0 0 ? * MON,TUE,WED,THU,FRI,SAT,SUN *', new ScheduleCreateRecharge ());

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

Thanks and Regards,
Deepali Kulshrestha