function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
AsokAsok 

How to call a Schedulable Class from Trigger?

Dear All,

 

I have Batch apex Class and Schedulable class.  I am looking for the syntax to call the Batch apex class from the trigger or calling the schedulale class from Trigger, which inturn calls the Batch apex.

 

Thanks in Advance!

Best Answer chosen by Admin (Salesforce Developers) 
zachelrathzachelrath

The syntax for launching a Batch Apex job from a Trigger is the same as launching it from anywhere else in Apex. 

 

Here's the syntax for launching your Scheduled Class, assuming it has a constructor:

 

//
// IN YOUR TRIGGER --- launch a Scheduled Apex class called "ScheduledClass"
//

// Build a CRON Expression corresponding to 1 second from now
Datetime executeTime = (System.now()).addSeconds(1);
String cronExpression = util_Utilities.GetCRONExpression(executeTime);
			
//System.debug('***Cron Expression: ' + cronExpression);
			
// Instantiate a new Scheduled Apex class
ScheduledClass scheduledJob = new ScheduledClass();
	        
// Schedule our class to run at our given execute time, 
// naming executeTime so that the the Schedule name will be Unique  
System.schedule('ScheduledJob ' + executeTime.getTime(),cronExpression,scheduledJob);


//
// UTILITY CLASS 
//

// Builds a CRON Expression out of a Datetime
public static String GetCRONExpression(Datetime dt) {
   return ('' + dt.second() + ' ' + dt.minute() + ' ' + dt.hour() + ' ' + dt.day() + ' ' + dt.month() + ' ? ' + dt.year());
}

 

I would be very careful, though, about launching your scheduled Job from a trigger, as you NEED, NEED to check to make sure that you have available Scheduled Jobs --- you can only have 10 Scheduled Jobs at a given time. Use something like this to check to make sure you have enough Jobs:

 

//
// UTILITY CLASS
//

public static final Integer MAX_SCHEDULED_JOBS	= 10;

// Determine whether the maximum number
// of Scheduled Jobs has been reached
public static Boolean MaxScheduledJobsReached() {
   return (GetScheduledJobs().size() >= MAX_SCHEDULED_JOBS) ;
}
	
// Returns all Scheduled Apex jobs that have not been started yet 
public static List<CronTrigger> GetScheduledJobs() {
   return [select Id, NextFireTime 
           from CronTrigger 
            where State in ('WAITING','ACQUIRED','EXECUTING')
           or NextFireTime != NULL];
}

 

 

As far as launching Batch Apex jobs, I usually have a utility method in my Batch classes that makes it easy to launch a new Batch Job, i.e.

 

//
// BATCH APEX CLASS
// 

global class MyBatchClass implements Database.Batchable<sObject>, Database.Stateful {

    Id accountId;

    public MyBatchClass(Id accountId) {
        this.accountId = accountId;
    }

    global void execute(...)
    global void finish(...)
    etc....

    // Utility method for kicking off a new batch job,
    // taking in a parameter
    public static Id StartJob(Id accountId) {
        MyBatchClass b = new MyBatchClass(accountId);
        return Database.executeBatch(b);
    }

}

//
// IN YOUR TRIGGER
//

Id accountId = '001000000AC799cDF';
MyBatchClass.StartJob(accountId);