+ Start a Discussion
SFDC16SFDC16 

How to call Scheduler class from trigger?

Hi,
How to call Scheduler class from the trigger?

Regards,
SFDC16
Jayanth ThathapudiJayanth Thathapudi
Hi 

We can call it but not recommended to use Scheduler/batches(10 Scheduled Jobs at a given time) in the triggers because they might trigger for any modification on the record.

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_scheduler.htm

Please mark as best answer if it helps you:)

Thanks,
Jay
Pankaj ShakyaPankaj Shakya
Hi SFDC16

In addition to what Jayanth has mentioned please refer to this link 
https://developer.salesforce.com/forums/?id=906F00000008zgyIAA

Regards
Pankaj Shakya
Raj VakatiRaj Vakati
You can 

 
trigger demo on Account(after insert){
Datetime executeTime = (System.now()).addSeconds(1); 
String cronExpression ='';

System.schedule('ScheduledJob ' ,cronExpression,new scheduledMonthly ());
}

global class scheduledMonthly implements Schedulable {
    /**
    * Builds up all of the new Objects
    *
    * @param sc The schedulable context
    */
    global void execute(SchedulableContext sc) {
        RecordType rt = [
            select Id
            from RecordType
            where DeveloperName = 'Recipient'
        ];

        List<MyObject__c> objectList = new List<MyObject__c>();

        //Get all of the accounts of type 'Recipient'
        for (Account account: [
            select Id
            from Account
            where RecordTypeId = :rt.Id
        ]) {
            objectList.add(new MyObject__c(
                Account__c = account.Id
            ));
        }

        if (!objectList.isEmpty()) {
            insert objectList;
        }
    }
}