Adding a Class that Implements the Schedulable Interface | Techila
830
single,single-post,postid-830,single-format-standard,ajax_updown_fade,page_not_loaded,,qode_grid_1300,footer_responsive_adv,hide_top_bar_on_mobile_header,qode-content-sidebar-responsive,qode-theme-ver-9.4.2,bridge,wpb-js-composer js-comp-ver-4.12,vc_responsive
 

Adding a Class that Implements the Schedulable Interface

Adding a Class that Implements the Schedulable Interface

In this lesson, you’ll write a class that implements the Schedulable interface, which means it can be scheduled to run at a specified date and time.

1.       In the Developer Console, click File | New | Apex Class.

2.       For the class name, enter MySchedulableClass and click OK.

3.       Delete the auto-generated code and add the following.

global class MySchedulableClass implements Schedulable {

global void execute(SchedulableContext ctx) {

CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime

FROM CronTrigger WHERE Id = :ctx.getTriggerId()];

 System.debug(ct.CronExpression);

 System.debug(ct.TimesTriggered);

 Merchandise__c m = new Merchandise__c(

 Name='Scheduled Job Item',

  Description__c='Created by the scheduler',

 Price__c=1,

  Total_Inventory__c=1000);

 insert m;

}

}

 

1.       Click File | Save.

·   The declaration of the class contains an extra implements Schedulable at the end. This indicates that the class implements the Schedulable interface and must implement the only method that this interface contains, which is this execute method:

1

global void execute(SchedulableContext sc){}

·    The parameter of this method is a SchedulableContext object. It provides the getTriggerId method that returns the ID of the CronTrigger API object. After a class has been scheduled, a CronTrigger object is created that represents the scheduled job.

·       The CronTrigger object is queried to get additional information about the scheduled job. The Cron expression and the number of times the job has been run already is written to the debug log.

·         Finally, the execute method creates a merchandise record.

Courtesy: https://developer.salesforce.com/docs/atlas.en-us.apex_workbook.meta/apex_workbook/apex_scheduling_1.htm

“Explore – Techila Global Services, A Salesforce development company”

Author: techila

No Comments

Post A Comment