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
Inbox OutboxInbox Outbox 

Can someone give me the syntax/ code of a schdulable class invoked from a trigger and the test class as well.?

Greetings!

Please give the class code and also the trigger code completely include the CRON expression. 
I don't know the syntax of how to invoke a trigger and then test it to run.

I just need the syntax to go by.

I'd appreciate the scenario of creating account from lead (account name with the lead's last name. 

Thanks a ton!

Suraj Tripathi 47Suraj Tripathi 47

Hi,

You can take reference from the below.

Requirement : "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."

Ans :

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);


Link :  https://salesforce.stackexchange.com/questions/148270/how-can-i-call-a-schedule-class-from-trigger-before-the-due-date-c-field

Link 2: https://salesforce.stackexchange.com/questions/22590/how-can-i-call-a-schedule-class-execute-method-from-normal-apex-class-or-apex-tr/26935

Link 3: Reference fro test class also

http://theblogreaders.com/write-schedule-apex-class-test-class-salesforce/

Please mark it as the best answer so that other people would take reference from it.

Thank You