schedule apex

Schedule Apex in Salesforce | Apex Developer Guide

Schedule Apex in Salesforce

  1. If you want to run apex classes at a specific time then we use schedule apex.
  2. If you want to schedule an apex class, then the apex class has to implement to the “Schedulable” Interface.
    • Syntax: Public class example implements Schedulable {}
  3. If any apex class which implements schedulable interface has to define the “execute” method
    • Syntax: Public void execute(schedulablecontext sc){}
  4. Operation or Logic which you want to schedule should be defined in the execute method.

Symbols and Meanings:

* Every (Everyday,EveryMonth, EveryHour,EveryMinute)

L Last (Lastday, LastMonth, lastFriday)

W Nearest working day

? Not Sure

, 3,5,7 (every 3rd,5th ,7th)

dont miss out iconDon't forget to check out: Types of Collections in Apex Salesforce | Explained

Example 1: Scheduling the apex at 1:53 PM on 7th Dec 2017, for deleting contacts which are created today

Apex Class:

public class Scheduleapex implements schedulable {
    public void execute(SchedulableContext sc){
        list<contact> accs=[select id,lastname from contact where createddate=TODAY];
        delete accs;
    }
}
// Code to schedule Apex Class 
Scheduleapex sa=new Scheduleapex();
string expression='0 53 13 7 12 ? 2017';
system.schedule('vipin',expression,sa);

Example2: Calling batch apex from schedule apex, and performing different DML operations.

public class Scheduleapex implements schedulable {
    public void execute(SchedulableContext sc){
        list<contact> accs=[select id,lastname from contact where createddate=TODAY];
        delete accs;
        BatchEx3 be=new BatchEx3();
        database.executeBatch(be,5);
    }
}
Global class BatchEx3 implements Database.Batchable<Sobject>{
    Global Database.QueryLocator start(database.BatchableContext bc ){
        Return Database.getQueryLocator('Select id from account where createddate=Last_Month');
    }
    Global void Execute(Database.BatchableContext bc,List<account> acc){
        for(integer i=0;i<50;i++){
            account a=new account();
            a.name='Test';
            a.Email__c='[email protected]';
            insert a;
        }
    }
    Global void Finish(Database.Batchablecontext bc){}
}
//Code to schedule
Scheduleapex sa=new Scheduleapex();
string expression='0 42 14 7 12 ? 2017';
system.schedule('vipin1', expression,sa);

Example3: Calling Batch apex, Queueable apex, future method from Schedule apex, and performing various DML operations. Apex Program:

// Schedule Apex
public class Scheduleapex implements schedulable {
    public void execute(SchedulableContext sc){
        list<contact> accs=[select id,lastname from contact where createddate=TODAY];
        delete accs;
        BatchEx3 be=new BatchEx3();//Calling batch apex
        database.executeBatch(be,5);
        ChainingApex2 ac=new ChainingApex2();//Calling Queueabe apex
        system.enqueueJob(ac);
        FutureAcc fa=new FutureAcc();//calling future method
        fa.accss();
    }
}
// Batch Apex
Global class BatchEx3 implements Database.Batchable<Sobject>{
    Global Database.QueryLocator start(database.BatchableContext bc ){
        Return Database.getQueryLocator('Select id from account where createddate=Last_Month');
    }
    Global void Execute(Database.BatchableContext bc,List<account> acc){
        for(integer i=0;i<50;i++){
            account a=new account();
            a.name='test1;
            a.Email__c='[email protected]';
            insert a;
        }
    }
    Global void Finish(Database.Batchablecontext bc){}
}

Queueable Apex:

public class ChainingApex2 implements Queueable {
    public void execute(queueablecontext qc1){
        list<account> acc2=[select name,phone,industry,fax,sic from account where
        industry='banking'];
        for(account b:acc2){
            b.fax='143143143';
            id x=qc1.getJobId();
            b.Sic=x;
            update b;
        }
        ChainingApex1 ca=new ChainingApex1();
        id ids=system.enqueueJob(ca);
    }
}
public class chainingApex1 implements Queueable {
    public void execute(queueablecontext qc){
        list<account> acc1=[select name,phone,industry,fax,description from account where
        industry='banking'];
        for(account a:acc1){
            a.phone='9999999';
            id x1=qc.getJobId();
            a.description=x1;
            update a;
        }
    }
}

dont miss out iconCheck out another amazing blog by Kirandeep here: Controller Extensions in Salesforce | The Developer Guide

Future Method:

public class FutureAcc {
    public void acc(){
        List<Account> acc=[select id,Email__c,fax,phone from Account where industry='banking'];
        for(account a:acc){
            // a.phone='+9999999';
            a.Email__c='[email protected]';
            update a;
        }
    }
    @future
    public static void accs(){
        List<Account> acc=[select id,Email__c,fax,phone from Account where industry='Education'];
        for(account a:acc){
            //a.fax='+911111111';
            a.Email__c='[email protected]';
            update a;
        }
    }
    public void accss(){
        acc();
        accs();
    }
}

Responses

Popular Salesforce Blogs