Apex @future アノテーションを使用して取引先レコードを更新する Apex クラスを作成する方法

環境
salesforce

機能
Account オブジェクトに項目を作成する:
表示ラベル: 取引先責任者数
名前: Number_Of_Contacts
データ型: 数値
この項目には取引先の取引先責任者の総数が保持される
Apex クラスを作成する:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
 名前: AccountProcessor
 メソッド名: countContacts
 メソッドは Account IDs のリストを受け入れる必要がある
 メソッドは @future アノテーションを使用する必要がある
 メソッドは渡された各 Account ID に関連付けられた取引先責任者レコードの数をカウントし、その値で [Number_Of_Contacts__c] 項目を更新する
 名前: AccountProcessor  メソッド名: countContacts  メソッドは Account IDs のリストを受け入れる必要がある  メソッドは @future アノテーションを使用する必要がある  メソッドは渡された各 Account ID に関連付けられた取引先責任者レコードの数をカウントし、その値で [Number_Of_Contacts__c] 項目を更新する
 名前: AccountProcessor
 メソッド名: countContacts
 メソッドは Account IDs のリストを受け入れる必要がある
 メソッドは @future アノテーションを使用する必要がある
 メソッドは渡された各 Account ID に関連付けられた取引先責任者レコードの数をカウントし、その値で [Number_Of_Contacts__c] 項目を更新する

Apex テストクラスを作成する:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
名前: AccountProcessorTest
  単体テストは AccountProcessor クラスに含まれるすべてのコード行をカバーし、結果のコードカバー率が 100% になる必要がある。
名前: AccountProcessorTest   単体テストは AccountProcessor クラスに含まれるすべてのコード行をカバーし、結果のコードカバー率が 100% になる必要がある。
名前: AccountProcessorTest
  単体テストは AccountProcessor クラスに含まれるすべてのコード行をカバーし、結果のコードカバー率が 100% になる必要がある。

操作方法
1.AccountProcessor クラスの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
public class AccountProcessor
{
@future
public static void countContacts(Set<id> setId)
{
    //Accountリストの宣言
List<Account> lstAccount = [select id,Number_of_Contacts__c , (select id from contacts ) from account where id in :setId ];
//リストの内容を出力
for( Account acc : lstAccount )
{
List<Contact> lstCont = acc.contacts ;
acc.Number_of_Contacts__c = lstCont.size();
}
// 更新リスト
update lstAccount;
}
}
public class AccountProcessor { @future public static void countContacts(Set<id> setId) {     //Accountリストの宣言 List<Account> lstAccount = [select id,Number_of_Contacts__c , (select id from contacts ) from account where id in :setId ]; //リストの内容を出力 for( Account acc : lstAccount ) { List<Contact> lstCont = acc.contacts ; acc.Number_of_Contacts__c = lstCont.size(); } // 更新リスト update lstAccount; } }
public class AccountProcessor 
{
  @future
  public static void countContacts(Set<id> setId) 
  {
      //Accountリストの宣言
      List<Account> lstAccount = [select id,Number_of_Contacts__c , (select id from contacts ) from account where id in :setId ];
         //リストの内容を出力
      for( Account acc : lstAccount )
      {
          List<Contact> lstCont = acc.contacts ;
          
          acc.Number_of_Contacts__c = lstCont.size();
      }
        // 更新リスト
      update lstAccount;
  }
}

2.テストクラスの定義

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
@IsTest
public class AccountProcessorTest {
public static testmethod void TestAccountProcessorTest()
{
   //Accountの宣言
Account a = new Account();
a.Name = 'Test Account';
Insert a;
//Contactの宣言
Contact cont = New Contact();
cont.FirstName ='Bob';
cont.LastName ='Masters';
cont.AccountId = a.Id;
Insert cont;
set<Id> setAccId = new Set<ID>();
setAccId.add(a.id);
System.test.startTest();
AccountProcessor.countContacts(setAccId);
System.test.stopTest();
Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];
System.assertEquals ( Integer.valueOf(ACC.Number_of_Contacts__c) ,1);
}
}
@IsTest public class AccountProcessorTest { public static testmethod void TestAccountProcessorTest() {    //Accountの宣言 Account a = new Account(); a.Name = 'Test Account'; Insert a; //Contactの宣言 Contact cont = New Contact(); cont.FirstName ='Bob'; cont.LastName ='Masters'; cont.AccountId = a.Id; Insert cont; set<Id> setAccId = new Set<ID>(); setAccId.add(a.id); System.test.startTest(); AccountProcessor.countContacts(setAccId); System.test.stopTest(); Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1]; System.assertEquals ( Integer.valueOf(ACC.Number_of_Contacts__c) ,1); } }
@IsTest
public class AccountProcessorTest {
    public static testmethod void TestAccountProcessorTest() 
    {
         //Accountの宣言
        Account a = new Account();
        a.Name = 'Test Account';
        Insert a;
            
            //Contactの宣言
        Contact cont = New Contact();
        cont.FirstName ='Bob';
        cont.LastName ='Masters';
        cont.AccountId = a.Id;
        Insert cont;
        
        set<Id> setAccId = new Set<ID>();
        setAccId.add(a.id);
 
       System.test.startTest();
            AccountProcessor.countContacts(setAccId);
        System.test.stopTest();
        
        Account ACC = [select Number_of_Contacts__c from Account where id = :a.id LIMIT 1];
        System.assertEquals ( Integer.valueOf(ACC.Number_of_Contacts__c) ,1);
  }
  
}

 

Apex

Posted by arkgame