Trailhead Batch Apex を使用してリードレコードを更新する Apex クラスを作成する方法

環境
Salesforce

実装機能
Database.Batchable インターフェースを実装して組織のすべてのリードレコードを特定の LeadSource で更新する Apex クラスを作成します。

Apex クラスを作成する:
名前: LeadProcessor
インターフェース: Database.Batchable
start メソッドで QueryLocator を使用して組織のすべての Lead レコードを収集する
execute メソッドでは組織のすべての Lead レコードを LeadSource 値「Dreamforce」で更新する必要がある
Apex テストクラスを作成する:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
  名前: LeadProcessorTest
  テストクラスで、200 件の Lead レコードを挿入し、LeadProcessor Batch クラスを実行し、すべての Lead レコードが正しく更新されたことを検証する
  単体テストは LeadProcessor クラスに含まれるすべてのコード行をカバーし、結果のコードカバー率が 100% になる必要がある
  名前: LeadProcessorTest   テストクラスで、200 件の Lead レコードを挿入し、LeadProcessor Batch クラスを実行し、すべての Lead レコードが正しく更新されたことを検証する   単体テストは LeadProcessor クラスに含まれるすべてのコード行をカバーし、結果のコードカバー率が 100% になる必要がある
  名前: LeadProcessorTest
  テストクラスで、200 件の Lead レコードを挿入し、LeadProcessor Batch クラスを実行し、すべての Lead レコードが正しく更新されたことを検証する
  単体テストは LeadProcessor クラスに含まれるすべてのコード行をカバーし、結果のコードカバー率が 100% になる必要がある

操作例
1.Apex クラスを作成する

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
global class LeadProcessor implements Database.Batchable<Sobject>
{
// Leadレコードを取得する
global Database.QueryLocator start(Database.BatchableContext bc)
{
return Database.getQueryLocator([Select LeadSource From Lead]);
}
//Leadレコードを更新
global void execute(Database.BatchableContext bc,List<Lead>scope)
{
for(Lead Leads : scope)
{
Leads.LeadSource = 'Dreamforce';
}
update scope;
}
global void finish(Database.BatchableContext bc){}
}
global class LeadProcessor implements Database.Batchable<Sobject> { // Leadレコードを取得する global Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator([Select LeadSource From Lead]); } //Leadレコードを更新 global void execute(Database.BatchableContext bc,List<Lead>scope) { for(Lead Leads : scope) { Leads.LeadSource = 'Dreamforce'; } update scope; } global void finish(Database.BatchableContext bc){} }
global class LeadProcessor implements Database.Batchable<Sobject> 
{
    // Leadレコードを取得する
    global Database.QueryLocator start(Database.BatchableContext bc)
    {
      return Database.getQueryLocator([Select LeadSource From Lead]);   
    }
       //Leadレコードを更新
    global void execute(Database.BatchableContext bc,List<Lead>scope)
    {
        for(Lead Leads : scope) 
        {
          Leads.LeadSource = 'Dreamforce';    
        }
        update scope;
    }
    global void finish(Database.BatchableContext bc){}    
}

2.単体テストクラスの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@isTest
public class LeadProcessorTest {
//テストメソッドの定義
static testMethod void testMethod1()
{
List<Lead>lstLead = new List<Lead>();
// 200件のLeadレコードの挿入
for(Integer i=0; i<200;i++)
{
Lead lad = new Lead();
lad.FirstName ='Yamada';
lad.LastName='taro';
lad.Company='enkou';
lstLead.add(lad);
}
//挿入
insert lstLead;
System.test.startTest();
LeadProcessor lp = new LeadProcessor();
//Batchクラスを実行する
Database.executeBatch(lp);
System.test.stopTest();
}
}
@isTest public class LeadProcessorTest { //テストメソッドの定義 static testMethod void testMethod1() { List<Lead>lstLead = new List<Lead>(); // 200件のLeadレコードの挿入 for(Integer i=0; i<200;i++) { Lead lad = new Lead(); lad.FirstName ='Yamada'; lad.LastName='taro'; lad.Company='enkou'; lstLead.add(lad); } //挿入 insert lstLead; System.test.startTest(); LeadProcessor lp = new LeadProcessor(); //Batchクラスを実行する Database.executeBatch(lp); System.test.stopTest(); } }
@isTest
public class LeadProcessorTest {
     //テストメソッドの定義
    static testMethod void testMethod1()
    {
        List<Lead>lstLead = new List<Lead>();
        
            // 200件のLeadレコードの挿入
        for(Integer i=0; i<200;i++)
        {
            Lead lad = new Lead();
            lad.FirstName ='Yamada';
            lad.LastName='taro';
            lad.Company='enkou';
            lstLead.add(lad);
            
        }
            //挿入
        insert lstLead;
        
        System.test.startTest();
          LeadProcessor lp = new LeadProcessor();
              //Batchクラスを実行する
          Database.executeBatch(lp);
        System.test.stopTest();
        
    }
}

 

Apex

Posted by arkgame