Schedule Apex



Apex Scheduler

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.

  • You can only have 100 scheduled Apex jobs at one time. 
  • You can evaluate your current count by viewing the Scheduled Jobs page in Salesforce and creating a custom view with a type filter equal to “Scheduled Apex”. 
  • You can also programmatically query the CronTrigger and CronJobDetail objects to get the count of Apex scheduled jobs.
==================================================================

How to Schedule a Class

Step-1: Take any Apex class like Batch Class or Queueable Class

global class UpdateRecordbatch implements Database.Batchable<sObject> 
{
    global Database.QueryLocator start(Database.BatchableContext BC) 
    {
        String query = 'SELECT Id,Name FROM Contact';
        
        return Database.getQueryLocator(query);
    }
    global void execute(Database.BatchableContext BC, List<contact> scope) 
    {
        for(contact a : scope)
        {
            a.LastName = a.LastName + 'Updated by Batch job';
        }
        update scope;
    }
    global void finish(Database.BatchableContext BC) {
    }
}

Step-2: Scheduler Class For Batch Apex

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

=============================================================

Step-3: Scheduling

There are two options we have scheduled the scheduler classes.
1) By System Scheduler.
2) By Developer console


System Scheduler.

Step 1) Click on Setup->Apex class. Then search the Schedule Apex button.



Step 2) Select the scheduler class and set Time like the 
below screenshot.



By Developer console: Execute the below code from the developer console:

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

=====================================================================

Like our post & bookmark this blog for your future learning. 

Any suggestions and improvements are most welcome, please comment your suggestions and improvement in the comment box. 

Happy Coding Sharing Is caring. 😀😀😀 


إرسال تعليق

Post a Comment (0)

أحدث أقدم