3

I have create batch class, i am calling in Developer Console but i want execute directly like when i schedule every monday. how to shedule this batch class global class contact_batch_update_Department implements

Database.Batchable<sobject>{
    global String [] email = new String[] {'vipin.indora1@gmail.com'};

    global Database.QueryLocator start(Database.BatchableContext bc){
        return Database.getQueryLocator('SELECT id, Name, Department FROM Contact');
    }

    global void execute(Database.BatchableContext bc, List<sObject> scope){
        List<Contact> conList = new List<Contact>();
         for(sObject con : scope){
             Contact newCon = (contact)con;
             newCon.Department = 'IT Department';
             conList.add(newCon);
         }
         if(conList.size()>0 && conList != null){
             Database.update(conList);
         }
   }

    global void finish(Database.BatchableContext bc){
        AsyncApexJob a = [Select a.TotalJobItems, a.Status, a.NumberOfErrors, a.JobType, a.JobItemsProcessed, a.ExtendedStatus, a.CreatedById, a.CompletedDate From AsyncApexJob a WHERE id = :BC.getJobId()];    

          Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
          mail.setToAddresses(email);
          mail.setReplyTo('vipin.indora1@gmail.com');
          mail.setSenderDisplayName('Apex Batch Processing Module');
          mail.setSubject('Batch Processing '+a.Status);
          mail.setPlainTextBody('The Batch Apex job processed  '+a.TotalJobItems+'\n batches with  '+a.NumberOfErrors+' failures '+'\n Job Item processed are '+a.JobItemsProcessed);
          Messaging.sendEmail(new Messaging.Singleemailmessage [] {mail});

    }
}
3

If you want to automate your Batch class then you need schedule it using a scheduler class.

create an Apex class that implements Schedulable interface and excecute your batch class.

global with sharing class contact_batch_update_Department implements Schedulable{
 global void execute(SchedulableContext sc) {
         contact_batch_update_Department  batchToExecute = new contact_batch_update_Department (); 
         database.executebatch(batchToExecute);

 }}

Now schedule this Apex class either salesforce standard sceduler or cron expression. enter image description here

Use Cron Expression:

contact_batch_update_Scheduler batchScheduler= new contact_batch_update_Scheduler();


String cronStr = '0 30 * * * ?';

System.schedule('Process Job', cronStr, batchScheduler);
1

Use the Apex scheduler if you have specific Apex classes that you want to run on a regular basis, or to run a batch Apex job using the Salesforce user interface. The scheduler runs as system—all classes are executed, whether or not the user has permission to execute the class.

To schedule jobs using the Apex scheduler:

  • Implement the Schedulable interface in an Apex class that instantiates the class you want to run.
  • From Setup, enter Apex Classes in the Quick Find box, select Apex Classes, and then click Schedule Apex.
  • Specify the name of a class that you want to schedule.
  • Specify how often the Apex class is to run.
  • For Weekly—specify one or more days of the week the job is to run (such as Monday and Wednesday).
  • For Monthly—specify either the date the job is to run or the day (such as the second Saturday of every month.)
  • Specify the start and end dates for the Apex scheduled class. If you specify a single day, the job only runs once.
  • Specify a preferred start time. The exact time the job starts depends on service availability.
  • Click Save.

Alternatively, you can call the System.scheduleBatch method to schedule the batch job to run once at a future time. For more details, see “Using the System.scheduleBatch Method”

0

look for System.scheduleBatch method. It allows to schedule batches without implementing schedulable interface. Please refer to Apex documentation https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.