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
Anil Kumar DevarapuAnil Kumar Devarapu 

Is it possible to initialize / Call the Schedule Class in a trigger

Hello EveryOne,

I am new to salelsforce. i have a basic knowledge on Schedulable classes, can i schedule a class(using cron expression for every 5hrs) using (after insert) Trigger. And Assuming that my Schedule class calls(creates an instance for) Another Batch Apex. i had known that, Every time of an insert of record a Job is Qued and I think All this can also be achived with simply After Insert Trigger(without batch and Schedular Class)

Is this really Possible to schedule a Class using(inside) a Trigger. Any one clarify me,
Thanks in Advance

Best Answer chosen by Anil Kumar Devarapu
Himanshu ParasharHimanshu Parashar
Hey Anil,

yes this is possible.
 
//
// 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());
}

Utility class
 
//
// 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];
}


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

 

All Answers

Himanshu ParasharHimanshu Parashar
Hey Anil,

yes this is possible.
 
//
// 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());
}

Utility class
 
//
// 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];
}


Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S. If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.

 
This was selected as the best answer
Anil Kumar DevarapuAnil Kumar Devarapu
That"s really what i need, Your snippet is working. Thanks alot!