Activity Forums Salesforce® Discussions Write a syntax and structure of scheduler class in Salesforce?

  • Suraj

    Member
    May 12, 2017 at 1:40 pm

    Hi Saurabh,

    global class ScheduleDemo implements Schedulable{
    global void execute(SchedulableContext sc){
    BatchClass b = new BatchClass();
    database.executeBatch(b);
    }
    }

    Hope this will help you.

  • shariq

    Member
    September 19, 2018 at 12:14 pm

    Hi,

    To invoke Apex classes to run at specific times, first implement the Schedulable interface for the class, then specify the schedule using either the Schedule Apex page in the Salesforce user interface, or the System.schedule method.

    To schedule an Apex class to run at regular intervals, first write an Apex class that implements the Salesforce-provided interface Schedulable.

    The scheduler runs as system—all classes are executed, whether or not the user has permission to execute the class.

    To monitor or stop the execution of a scheduled Apex job using the Salesforce user interface, from Setup, enter Scheduled Jobs in the Quick Find box, then select Scheduled Jobs.

    The Schedulable interface contains one method that must be implemented, execute.

    global void execute(SchedulableContext sc){}

    The implemented method must be declared as global or public.

    The following example implements the Schedulable interface for a class called mergeNumbers:

    global class scheduledMerge implements Schedulable {
    global void execute(SchedulableContext SC) {
    mergeNumbers M = new mergeNumbers();
    }
    }

    The following example uses the System.Schedule method to implement the above class.

    scheduledMerge m = new scheduledMerge();
    String sch = '20 30 8 10 2 ?';
    String jobID = system.schedule('Merge Job', sch, m);

    You can also use the Schedulable interface with batch Apex classes. The following example implements the Schedulableinterface for a batch Apex class called batchable:

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

    Thanks

  • Avnish Yadav

    Member
    September 19, 2018 at 12:23 pm

    Hello,

    Here is simple structure of Schedular Class-

    global class BatchScheduleUpdate implements Schedulable
    {
        global void execute(SchedulableContext sc)
        {
            // Implement any logic to be scheduled
           
            // We now call the batch class to be scheduled
            BatchContactUpdate b = new BatchContactUpdate ();
           
            //Parameters of ExecuteBatch(context,BatchSize)
            database.executebatch(b,200);
        }
       
    }/pre>
    
    Thanks.
    • This reply was modified 5 years, 7 months ago by  Avnish Yadav.
  • Parul

    Member
    September 19, 2018 at 12:29 pm

    Here some code snippet:

    Apex scheduler is used to invoke Apex classes to run at specific times, first implement the Schedulable interface for the class, then specify the schedule using either the Schedule Apex page in the Salesforce user interface, or the System.schedule method.

    The Schedulable interface contains one method that must be implemented, execute.
    global void execute(SchedulableContext sc){}
    The implemented method must be declared as global or public.

    The following example implements the Schedulable interface for a class called mergeNumbers:
    global class scheduledMerge implements Schedulable{
    global void execute(SchedulableContext SC) {
    mergeNumbers M = new mergeNumbers();
    }
    }
    The following example uses the System.Schedule method to implement the above class.
    scheduledMerge m = new scheduledMerge();
    String sch = '20 30 8 10 2 ?';
    system.schedule('Merge Job', sch, m);
    You can also use the Schedulable interface with batch Apex classes. The following example implements the Schedulable interface for a batch Apex class called batchable:
    global class scheduledBatchable implements Schedulable{
    global void execute(SchedulableContext sc) {
    batchable b = new batchable();
    database.executebatch(b);
    }
    }

    Thanks

Log In to reply.

Popular Salesforce Blogs