Schedulable接口
定时任务调用需要单独写一个ApexClass,实现Schedulable接口,并重写execute方法,举例如下
ExampleUpdateBatch.cls
代码语言:javascript复制global with sharing class ExampleUpdateBatch implements Database.Batchable<sObject>,Database.Stateful,Database.AllowsCallouts {
public final String Query;
public ExampleUpdateBatch(String q) {
Query=q;
}
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator(Query);
}
global void execute(Database.BatchableContext BC, list<sObject> scope) {
Savepoint sp = Database.setSavepoint();
try {
List<Opportunity> newOppList = new List<Opportunity>();
for(Opportunity opportunityItem : (List<Opportunity>)scope) {
opportunityItem.stageName = 'Closed Won';
newOppList.add(opportunityItem);
}
update newOppList;
} catch (Exception ex) {
Database.rollback(sp);
}
}
global void finish(Database.BatchableContext BC) {
}
}
定时任务类
ExampleUpdateBatchSchedulable.cls
代码语言:javascript复制global class ExampleUpdateBatchSchedulable implements Schedulable{
public ExampleUpdateBatchSchedulable() {
}
global void execute(SchedulableContext ctx) {
String queryS = 'SELECT Id,Name FROM Opportunity WHERE DeleteFlg__c = true';
ExampleUpdateBatch batchTest = new ExampleUpdateBatch(queryS);
Database.executeBatch(batchTest, 200);
}
}
测试一下上边定时任务类↓↓↓
1.准备数据:
2.设置时间
可以在Scheduled Jobs中查看任务。
System.schedule用于调用任务类
举例如下:
代码语言:javascript复制@isTest
public with sharing class ExampleUpdateBatchSchedulableTest {
@TestSetup
static void initialOrgInfo(){
String strUserName = 'BatchTest001';
Profile profile = [SELECT Id FROM Profile WHERE Name = 'System Administrator' LIMIT 1];
User batchTestUser = new User();
batchTestUser.ProfileId = profile.Id;
batchTestUser.UserName = strUserName '@sample.com';
batchTestUser.FirstName = '';
batchTestUser.LastName = strUserName;
batchTestUser.EMail = 'testuser@sample.com';
batchTestUser.Alias = 'testuser';
batchTestUser.TimeZoneSidKey = 'Asia/Tokyo';
batchTestUser.LocaleSidKey = 'ja_JP';
batchTestUser.EmailEncodingKey = 'ISO-2022-JP';
batchTestUser.LanguageLocaleKey = 'ja';
Database.insert(batchTestUser, false);
PermissionSet permissionSet = [SELECT Id
FROM PermissionSet
WHERE Name = 'Ursus_Park_User'];
PermissionSetAssignment assignmentAss = new PermissionSetAssignment();
assignmentAss.AssigneeId = batchTestUser.Id;
assignmentAss.PermissionSetId = permissionSet.Id;
Database.insert(assignmentAss, false);
}
static testMethod void myTest001() {
User runUser = [SELECT ID, UserName FROM User WHERE username='BatchTest001@sample.com'];
System.runAs(runUser) {
Test.startTest();
List <Opportunity> oppList = new List<Opportunity>();
for(integer i = 0; i<200; i ){
Opportunity oppItem = new Opportunity(Name='testOpportunity' i,
StageName='Perception Analysis',
Ownerid = runUser.Id,
CloseDate = Date.Today(),
DeleteFlg__c = true);
oppList.add(oppItem);
}
insert oppList;
String executeTime = '20 30 8 10 2 ?';
ExampleUpdateBatchSchedulable exampleSchedule = new ExampleUpdateBatchSchedulable();
String jobId = System.schedule('batch test', executeTime, exampleSchedule);
CronTrigger ct = [SELECT Id, CronExpression, TimesTriggered, NextFireTime
FROM CronTrigger WHERE id = :jobId];
// Verify the expressions are the same
System.assertEquals(executeTime, ct.CronExpression);
// Verify the job has not run
System.assertEquals(0, ct.TimesTriggered);
// Verify the next time the job will run
System.assertEquals('2022-02-10 08:30:20', String.valueOf(ct.NextFireTime));
Test.stopTest();
}
}
}
关于执行时间的定义,顺序如下:
Seconds Minutes Hours Day_of_month Month Day_of_week Optional_year