Apex スケジューラーを使用したジョブのスケジュールのサンプル
環境
Salesforce Apex
スケジュール済み Apex の構文
public class SomeClass implements Schedulable {
public void execute(SchedulableContext ctx) {
// 処理コード
}
}
public class SomeClass implements Schedulable {
public void execute(SchedulableContext ctx) {
// 処理コード
}
}
public class SomeClass implements Schedulable { public void execute(SchedulableContext ctx) { // 処理コード } }
課題
スケジュール済み Apex を使用してリードレコードを更新する Apex クラスを作成する。
Schedulable インターフェースを実装してリードレコードを特定の LeadSource で更新する Apex クラスを作成します (Batch Apex の内容とよく似ています)。
Apex クラスを作成する:
名前: DailyLeadProcessor
インターフェース: Schedulable
execute メソッドでは LeadSource 項目が空白である Lead レコードの最初の 200 件を検出し、LeadSource 値「Dreamforce」で更新する必要がある
Apex テストクラスを作成する:
名前: DailyLeadProcessorTest
テストクラスで、200 件の Lead レコードを挿入し、DailyLeadProcessor クラスをスケジュールして実行し、すべての Lead レコードが正しく更新されたことを検証する
単体テストは DailyLeadProcessor クラスに含まれるすべてのコード行をカバーし、結果のコードカバー率が 100% になる必要がある。
この Challenge の完了を確認する前に、Developer Console の [Run All (すべて実行)] 機能を使用して、少なくとも 1 回テストクラスを実行する
スケジュール済み Apex を使用してリードレコードを更新する Apex クラスを作成する。
Schedulable インターフェースを実装してリードレコードを特定の LeadSource で更新する Apex クラスを作成します (Batch Apex の内容とよく似ています)。
Apex クラスを作成する:
名前: DailyLeadProcessor
インターフェース: Schedulable
execute メソッドでは LeadSource 項目が空白である Lead レコードの最初の 200 件を検出し、LeadSource 値「Dreamforce」で更新する必要がある
Apex テストクラスを作成する:
名前: DailyLeadProcessorTest
テストクラスで、200 件の Lead レコードを挿入し、DailyLeadProcessor クラスをスケジュールして実行し、すべての Lead レコードが正しく更新されたことを検証する
単体テストは DailyLeadProcessor クラスに含まれるすべてのコード行をカバーし、結果のコードカバー率が 100% になる必要がある。
この Challenge の完了を確認する前に、Developer Console の [Run All (すべて実行)] 機能を使用して、少なくとも 1 回テストクラスを実行する
スケジュール済み Apex を使用してリードレコードを更新する Apex クラスを作成する。 Schedulable インターフェースを実装してリードレコードを特定の LeadSource で更新する Apex クラスを作成します (Batch Apex の内容とよく似ています)。 Apex クラスを作成する: 名前: DailyLeadProcessor インターフェース: Schedulable execute メソッドでは LeadSource 項目が空白である Lead レコードの最初の 200 件を検出し、LeadSource 値「Dreamforce」で更新する必要がある Apex テストクラスを作成する: 名前: DailyLeadProcessorTest テストクラスで、200 件の Lead レコードを挿入し、DailyLeadProcessor クラスをスケジュールして実行し、すべての Lead レコードが正しく更新されたことを検証する 単体テストは DailyLeadProcessor クラスに含まれるすべてのコード行をカバーし、結果のコードカバー率が 100% になる必要がある。 この Challenge の完了を確認する前に、Developer Console の [Run All (すべて実行)] 機能を使用して、少なくとも 1 回テストクラスを実行する
1.Apexクラスの定義
global class DailyLeadProcessor implements Schedulable {
global void execute(SchedulableContext ctx) {
List<Lead> leadLst = [SELECT Id,LeadSource FROM Lead WHERE LeadSource = NULL LIMIT 200];
List<Lead> updLst = new List<Lead>();
for ( Lead ld : leadLst ) {
if(ld.LeadSource == NULL){
ld.LeadSource = 'Dreamforce';
updLst.add(l);
}
}
Update updLst;
}
}
global class DailyLeadProcessor implements Schedulable {
global void execute(SchedulableContext ctx) {
List<Lead> leadLst = [SELECT Id,LeadSource FROM Lead WHERE LeadSource = NULL LIMIT 200];
List<Lead> updLst = new List<Lead>();
for ( Lead ld : leadLst ) {
if(ld.LeadSource == NULL){
ld.LeadSource = 'Dreamforce';
updLst.add(l);
}
}
Update updLst;
}
}
global class DailyLeadProcessor implements Schedulable { global void execute(SchedulableContext ctx) { List<Lead> leadLst = [SELECT Id,LeadSource FROM Lead WHERE LeadSource = NULL LIMIT 200]; List<Lead> updLst = new List<Lead>(); for ( Lead ld : leadLst ) { if(ld.LeadSource == NULL){ ld.LeadSource = 'Dreamforce'; updLst.add(l); } } Update updLst; } }
2.テストクラスの定義
@isTest
public class DailyLeadProcessorTest {
@testSetup
static void setup(){
List<Lead> leadLst = new List<Lead>();
for ( Integer i = 0; i<200; i++){
leadLst.add(new Lead(
LastName = 'Test' +i,
Company = 'Name' + i,
LeadSource = NULL
));
}
Insert leadLst;
}
@isTest
static void testMethod1(){
List<Lead> leadLst = [SELECT Id FROM Lead LIMIT 200];
String CRON_EXP = '0 0 0 14 4 ? 2024';
System.Test.startTest();
String jobId = System.schedule('DailyLeadProcessor',
CRON_EXP,
new DailyLeadProcessor());
System.Test.stopTest();
}
}
@isTest
public class DailyLeadProcessorTest {
@testSetup
static void setup(){
List<Lead> leadLst = new List<Lead>();
for ( Integer i = 0; i<200; i++){
leadLst.add(new Lead(
LastName = 'Test' +i,
Company = 'Name' + i,
LeadSource = NULL
));
}
Insert leadLst;
}
@isTest
static void testMethod1(){
List<Lead> leadLst = [SELECT Id FROM Lead LIMIT 200];
String CRON_EXP = '0 0 0 14 4 ? 2024';
System.Test.startTest();
String jobId = System.schedule('DailyLeadProcessor',
CRON_EXP,
new DailyLeadProcessor());
System.Test.stopTest();
}
}
@isTest public class DailyLeadProcessorTest { @testSetup static void setup(){ List<Lead> leadLst = new List<Lead>(); for ( Integer i = 0; i<200; i++){ leadLst.add(new Lead( LastName = 'Test' +i, Company = 'Name' + i, LeadSource = NULL )); } Insert leadLst; } @isTest static void testMethod1(){ List<Lead> leadLst = [SELECT Id FROM Lead LIMIT 200]; String CRON_EXP = '0 0 0 14 4 ? 2024'; System.Test.startTest(); String jobId = System.schedule('DailyLeadProcessor', CRON_EXP, new DailyLeadProcessor()); System.Test.stopTest(); } }