Sample Code to Schedule an Apex class

I’ll be working on creating a apex schedule class and I just need a pointer to start is that possible if you can have a working small sample where I can understand how the mechanics work and I have googled but not find what exactly I want and also I looked at the salesforce site


Would this help? Then you’ll have to set the schedule in your apex classes setup

    global class Miko_Scheduled Implements Schedulable
        global void execute(SchedulableContext sc)

        public void setToMiko()
            List<Account> listAccounts = new List<Account>();
            listAccounts = [SELECT ID,Tenant_Name__C FROM Account WHERE Tenant_Name__C = null];

            for(Account acc : listAccounts)
                acc.Tenant_Name__C = 'Miko';

            update listAccounts;

afterwards you can go to Setup > Build > Develop > Apex Classes and then press on the ‘Schedule Apex’ to set up your class.


It’s helpful to add a few functions like the following to schedulable classes:

public static String schedmon5am = '0 00 05 ? * 2';  //Every Monday, 5AM
// used for scheduling a repeating job - currently set for Monday 5AM.
global static String scheduleMeMon5AM() {
    AutoChatterSiteManagerCheck SC = new AutoChatterSiteManagerCheck(); 
    return System.schedule('ScheduleClassNameStr - Monday 5AM', schedmon5am, SC);

That way, you can schedule the class from the “execute anonymous” window with the following:


without having to look up how scheduling strings work.

Source : Link , Question Author : Nick Kahn , Answer Author : Jagular

Leave a Comment