About
Batch Apex
Implementing
the Database.Batchable Interface
The
Database.Batchable interface contains three methods that must be implemented:
•
start method
global
(Database.QueryLocator | Iterable<sObject>)
start(Database.BatchableContext bc)
{}
The
start method is called at the beginning of a batch Apex job. Use the start
method to collect the records or objectsto be passed to the interface method
execute.This method returns either a Database.QueryLocator object or an iterable that contains the records
or objects being passed into the job.
Custom
Cloud Batch ApexUse the Database.QueryLocator object when you are using a
simple query (SELECT) to generate the scope of objects used in the batch job.
If you use a QueryLocator object, the governor limit for the total number of
records retrieved by SOQL queries is bypassed. For example, a batch Apex job
for the Account object can return a QueryLocator for all account records (up to
50 million records) in an organization. Another example is a sharing recalculation
for the Contact object that returns
a
QueryLocator for all contact records in an organization. Use the iterable when
you need to create a complex scope for the batch job. You can also use the
iterable to create your own custom process for iterating through the list. Important:
If you use an iterable, the governor limit for the total number of records
retrieved by SOQL queries is still enforced.
•
execute method:
global
void execute(Database.BatchableContext BC, list<P>){}
The
execute method is called for each batch of records passed to the method. Use
this method to do all required processing
for
each chunk of data.
This
method takes the following:
-
A reference to the Database.BatchableContext object.
-
A list of sObjects, such as List<sObject>, or a list of parameterized
types. If you are using a Database.QueryLocator, the returned list should be
used.
•
finish method
global
void finish(Database.BatchableContext BC){}
The
finish method is called after all batches are processed. Use this method to
send confirmation emails or execute post-processing operations.
Each
execution of a batch Apex job is considered a discrete transaction. For
example, a batch Apex job that contains 1,000
records
and is executed without the optional scope parameter from Database.executeBatch
is considered five transactions
of
200 records each.The Apex governor limits are reset for each transaction. If
the first transaction succeeds but the second
fails,
the database updates made in the first transaction are not rolled back.
Apex
Scheduler—Limited Release
You
asked for it! This enhancement is an idea from the IdeaExchange.
Available
in: Unlimited, Developer, and Enterprise Editions
You
can schedule Apex classes to run at specific times. First implement the
Schedulable interface for the class, then specify
the
schedule using either the Schedule Apex page in the Salesforce.com user
interface, or the System.schedule method.
In
addition, you can use the new method System.abortJob to stop a job after it has
been scheduled.
Note:
The Apex scheduler is currently available through a limited release program.
Contact salesforce.com to verify
if
your organization can participate in the limited release program.
143
Custom
Cloud Apex Scheduler—Limited ReleaseImplementation Tips
•
You must implement the Schedulable interface for the class first before you can
schedule the class.
•
Use either the System.schedule method or the Schedule Apex page in the
Salesforce.com user interface to schedule
the
class.
•
After you schedule a job, you can monitor the progress of the job on the All
Scheduled Jobs page.
•
Salesforce.com only adds the process to the queue at the scheduled time. Actual
execution may be delayed based on service
availability.
•
Once the job has completed, you can see specifics about the job (such as
whether it passed or failed, how long it took to
process,
the number of records process, and so on) on the Apex Jobs page.
•
The System.schedule method uses the user's timezone for the basis of all
schedules.
•
Use the System.abortJob to stop jobs that were scheduled using System.schedule.
Best
Practices
•
Use extreme care if you are planning to schedule a class from a trigger. You
must be able to guarantee that the trigger will
not
add more scheduled classes than the ten that are allowed. In particular,
consider API bulk updates, import wizards,
mass
record changes through the user interface, and all cases where more than one
record can be updated at a time.
•
Though it's possible to do additional processing in the execute method,Salesforce.com
recommends that all processing
take
place in a separate class.
About
Apex Scheduler
Implementing
the Schedulable Interface
To
schedule an Apex class to run at regular intervals, first write an Apex class
that implements the Salesforce.com-provided
interface
Schedulable.
To
monitor or stop the execution of a scheduled Apex job using the Salesforce.com
user interface, click Setup➤ Monitoring
➤ Scheduled Jobs. For more
information, see “Monitoring Scheduled Jobs” in the Salesforce.com online help.
To
stop execution of a job that was scheduled using the System.schedule method,
use the System.abortJob method.
The
Schedulable interface contains one method that must be implemented, execute.
global
void execute(SchedulableContext sc){}
Use
the SchedulableContext object to keep track of the scheduled job once it's
scheduled.The SchedulableContext method
getTriggerId
returns the Id of the CronTrigger object associated with this scheduled job as
a string. Use this method to
track
the progress of the scheduled job.
Use
this method to instantiate the class you want to schedule.
Tip:
Though it's possible to do additional processing in the execute method,
Salesforce.com recommends that all
processing
take place in a separate class.
The
following example implements the Schedulable interface for a class called
mergeNumbers:
global
class scheduledMerge implements Schedulable{
global
void execute(SchedulableContext SC) {
mergeNumbers
M = new mergeNumbers();
144
Custom
Cloud Apex Scheduler—Limited Release}
}
The
following example uses the System.Schedule method to implement the above class.
scheduledMerge
m = new scheduledMerge();
String
sch = '20 30 8 10 2 ?';
system.schedule('Merge
Job', sch, m);
You
can also use the Schedulable interface with batch Apex classes.The following
example implements the Schedulable
interface
for a batch Apex class called batchable:
global
class scheduledBatchable implements Schedulable{
global
void execute(SchedulableContext sc) {
batchable
b = new batchable();
database.executebatch(b);
}
}
Using
the System.Schedule Method
Note:
The Apex scheduler is currently available through a limited release program.
Contact salesforce.com to verify
if
your organization can participate in the limited release program.
After
you implement a class with the Schedulable interface, use the System.Schedule
method to execute it.
Note:
Use extreme care if you are planning to schedule a class from a trigger. You
must be able to guarantee that the
trigger
will not add more scheduled classes than the ten that are allowed. In
particular, consider API bulk updates,
import
wizards, mass record changes through the user interface, and all cases where
more than one record can be
updated
at a time.
The
System.Schedule method takes three arguments: a name for the job, an expression
used to represent the time and
date
the job is scheduled to run, and the name of the class.This expression has the
following syntax:
Seconds
Minutes Hours Day_of_month Month Day_of_week optional_year
Note:
Salesforce.com only adds the process to the queue at the scheduled time. Actual
execution may be delayed based
on
service availability.
The
System.Schedule method uses the user's timezone for the basis of all schedules.
The
following are the values for the expression:
Name
Values Special Characters
Seconds
0–59 None
Minutes
0–59 None
Hours
0–23 , - * /
Day_of_month
1–31 , - * ? / L W
Month
1–12 or the following: , - * /
•
JAN
•
FEB
145
Custom
Cloud Apex Scheduler—Limited ReleaseName Values Special Characters
•
MAR
•
APR
•
MAY
•
JUN
•
JUL
•
AUG
•
SEP
•
OCT
•
NOV
•
DEC
Day_of_week
1–7 or the following: , - * ? / L #
•
SUN
•
MON
•
TUE
•
WED
•
THU
•
FRI
•
SAT
optional_year
null or 1970–2099 , - * /
The
special characters are defined as follows:
•
, —Delimits values. For example, use JAN, MAR, APR to specify more than one
month.
•
- —Specifies a range. For example, use JAN-MAR to specify more than one month.
•
* —Specifies all values. For example, if Month is specified as *, the job is
scheduled for every month.
•
? —Specifies no specific value.This is only available for Day_of_month and
Day_of_week, and is generally used when
specifying
a value for one and not the other.
•
/ —Specifies increments.The number before the slash specifies when the
intervals should begin, and the number after
the
slash is the interval amount. For example, if you specify 1/5 for Day_of_month,
the Apex class runs every fifth day
of
the month, starting on the first of the month.
•
L —Specifies the end of a range (last).This is only available for Day_of_month
and Day_of_week. When used with
Day
of month, L always means the last day of the month, such as January 31,
February 28 for leap years, and so on.
When
used with Day_of_week by itself, it always means 7 or SAT. When used with a
Day_of_week value, it means the
last
of that type of day in the month. For example, if you specify 2L, you are
specifying the last Monday of the month. Do
not
use a range of values with L as the results may be unexpected.
•
W —Specifies the nearest weekday (Monday-Friday) of the given day.This is only
available for Day_of_month. For
example,
if you specify 20W, and the 20th is a Saturday, the class runs on the 19th. If
you specify 1W, and the first is a
Saturday,
the class does not run in the previous month, but on the third, which is the
following Monday.
Tip:
Use the L and W together to specify the last weekday of the month.
•
# —Specifies the nth day of the month, in the format weekday#day_of_month.This
is only available for Day_of_week.
The
number before the # specifies weekday (SUN-SAT).The number after the #
specifies the day of the month. For
example,
specifying 2#2 means the class runs on the second Monday of every month.
146
Custom
Cloud Apex Scheduler—Limited ReleaseThe following are some examples of how to
use the expression.
Expression
Description
0
0 13 * * ? Class runs every day at 1 P.M.
0
0 22 ? * 6L Class runs the last Friday of every month at 10 P.M.
0
0 10 ? * MON-FRI Class runs Monday through Friday at 10 A.M.
0
0 20 * * ? 2010 Class runs every day at 8 P.M. during the year 2010.
In
the following example, the class proschedule implements the Schedulable
interface.The class is scheduled to run at
8
A.M., on the 13th of February.
proschedule
p = new proschedule();
String
sch = '0 0 8 13 2 ?';
system.schedule('One
Time Pro', sch, p);
Scheduling
Apex
Available
in: Unlimited, Developer, and Enterprise Editions
Note:
The Apex scheduler is currently available through a limited release program.
Contact salesforce.com to verify
if
your organization can participate in the limited release program.
Use
the Apex scheduler if you have specific Apex classes that you want to run on a
regular basis, or to run a batch Apex job
using
the Salesforce.com user interface.
Important:
Salesforce.com only adds the process to the queue at the scheduled time. Actual
execution may be delayed
based
on service availability.
To
schedule jobs using the Apex scheduler:
1.
Implement the Schedulable interface in an Apex class that instantiates the
class you want to run.
2.
Click Setup ➤ Develop ➤
Apex Classes and click Schedule Apex.
3.
Specify the name of a class that you want to schedule.
4.
Specify how often the Apex class is to run.
•
For Weekly—specify one or more days of the week the job is to run (such as
Monday and Wednesday).
•
For Monthly—specify either the date the job is to run or the day (such as the
second Saturday of every month.)
5.
Specify the start and end dates for the Apex scheduled class. If you specify a
single day, the job only runs once.
6.
Specify a preferred start time.The exact time the job starts depends on what
other jobs are in the queue at that time.
7.
Click Save.
Note:
You can only have ten active or scheduled jobs concurrently.
147
Custom
Cloud Apex Scheduler—Limited ReleaseAfter you schedule an Apex job, you can
monitor the progress of the job on the All Scheduled Jobs page.
Once
the job has completed, you can see specifics about the job (such as whether it
passed or failed, how long it took to process,
the
number of records process, and so on) on the Apex Jobs page.
No comments:
Post a Comment