Apex future メソッドの使用方法
環境
Apex Salesforce
課題
@futureアノテーションを使用してアカウントレコードを更新するApexクラスを作成します。
アカウントIDのリストを受け入れ、アカウントに関連付けられた連絡先の数でAccountオブジェクトのカスタムフィールドを更新する@futureアノテーションを使用するメソッドでApexクラスを作成します。クラスのコードカバレッジを100%達成する単体テストを作成します。
タイプNumberの「Number_of_Contacts__c」という名前のフィールドをAccountオブジェクトに作成します。このフィールドには、アカウントの連絡先の総数が保持されます。
アカウントIDのリストを受け入れる「countContacts」メソッドを含む「AccountProcessor」というApexクラスを作成します。このメソッドは@futureアノテーションを使用する必要があります。
メソッドに渡されたアカウントIDごとに、それに関連付けられている連絡先レコードの数をカウントし、「Number_of_Contacts__c」フィールドをこの値で更新します。
‘AccountProcessorTest’というApexテストクラスを作成します。
単体テストは、AccountProcessorクラスに含まれるコードのすべての行をカバーする必要があり、100%のコードカバレッジが得られます。
このチャレンジを検証する前に、テストクラスを少なくとも1回実行します(「すべて実行」を使用して開発者コンソールをテストします)。
タイプNumberの「Number_of_Contacts__c」という名前のフィールドをAccountオブジェクトに作成します。このフィールドには、アカウントの連絡先の総数が保持されます。
アカウントIDのリストを受け入れる「countContacts」メソッドを含む「AccountProcessor」というApexクラスを作成します。このメソッドは@futureアノテーションを使用する必要があります。
メソッドに渡されたアカウントIDごとに、それに関連付けられている連絡先レコードの数をカウントし、「Number_of_Contacts__c」フィールドをこの値で更新します。
‘AccountProcessorTest’というApexテストクラスを作成します。
単体テストは、AccountProcessorクラスに含まれるコードのすべての行をカバーする必要があり、100%のコードカバレッジが得られます。
このチャレンジを検証する前に、テストクラスを少なくとも1回実行します(「すべて実行」を使用して開発者コンソールをテストします)。
タイプNumberの「Number_of_Contacts__c」という名前のフィールドをAccountオブジェクトに作成します。このフィールドには、アカウントの連絡先の総数が保持されます。 アカウントIDのリストを受け入れる「countContacts」メソッドを含む「AccountProcessor」というApexクラスを作成します。このメソッドは@futureアノテーションを使用する必要があります。 メソッドに渡されたアカウントIDごとに、それに関連付けられている連絡先レコードの数をカウントし、「Number_of_Contacts__c」フィールドをこの値で更新します。 ‘AccountProcessorTest’というApexテストクラスを作成します。 単体テストは、AccountProcessorクラスに含まれるコードのすべての行をカバーする必要があり、100%のコードカバレッジが得られます。 このチャレンジを検証する前に、テストクラスを少なくとも1回実行します(「すべて実行」を使用して開発者コンソールをテストします)。
使用例
取引先の取引先連絡者数を集計する必要があるので、COUNTを使用します。
それをAggregateResultオブジェクトに格納して代入します。
public class AccountProcessor {
@future
public static void countContacts(List<id> accIds) {
List<Account> accList = new List<Account>();
for (AggregateResult ar : [SELECT AccountId a, COUNT(Id) c
FROM Contact
WHERE AccountId in :accIds
GROUP BY AccountId
]) {
accList.add(
new Account(
Id = (Id) ar.get('a'),
Number_of_Contacts__c = (Decimal) ar.get('c')
));
}
update accList;
}
}
public class AccountProcessor {
@future
public static void countContacts(List<id> accIds) {
List<Account> accList = new List<Account>();
for (AggregateResult ar : [SELECT AccountId a, COUNT(Id) c
FROM Contact
WHERE AccountId in :accIds
GROUP BY AccountId
]) {
accList.add(
new Account(
Id = (Id) ar.get('a'),
Number_of_Contacts__c = (Decimal) ar.get('c')
));
}
update accList;
}
}
public class AccountProcessor { @future public static void countContacts(List<id> accIds) { List<Account> accList = new List<Account>(); for (AggregateResult ar : [SELECT AccountId a, COUNT(Id) c FROM Contact WHERE AccountId in :accIds GROUP BY AccountId ]) { accList.add( new Account( Id = (Id) ar.get('a'), Number_of_Contacts__c = (Decimal) ar.get('c') )); } update accList; } }
テストクラスの定義
@IsTest
public class AccountProcessorTest {
public static testmethod void TestAccountProcessorTest() {
Account acc = new Account();
acc.Name = 'Test Account';
Insert acc;
Contact ct = New Contact();
ct.LastName ='Test Contact';
ct.AccountId = acc.Id;
Insert ct;
List<Id> setAccId = new List<Id>();
setAccId.add(acc.id);
System.Test.startTest();
AccountProcessor.countContacts(setAccId);
System.Test.stopTest();
Account acct = [SELECT Number_of_Contacts__c FROM Account WHERE id = :acc.id LIMIT 1];
System.assertEquals ( Integer.valueOf(acct.Number_of_Contacts__c) ,1);
}
}
@IsTest
public class AccountProcessorTest {
public static testmethod void TestAccountProcessorTest() {
Account acc = new Account();
acc.Name = 'Test Account';
Insert acc;
Contact ct = New Contact();
ct.LastName ='Test Contact';
ct.AccountId = acc.Id;
Insert ct;
List<Id> setAccId = new List<Id>();
setAccId.add(acc.id);
System.Test.startTest();
AccountProcessor.countContacts(setAccId);
System.Test.stopTest();
Account acct = [SELECT Number_of_Contacts__c FROM Account WHERE id = :acc.id LIMIT 1];
System.assertEquals ( Integer.valueOf(acct.Number_of_Contacts__c) ,1);
}
}
@IsTest public class AccountProcessorTest { public static testmethod void TestAccountProcessorTest() { Account acc = new Account(); acc.Name = 'Test Account'; Insert acc; Contact ct = New Contact(); ct.LastName ='Test Contact'; ct.AccountId = acc.Id; Insert ct; List<Id> setAccId = new List<Id>(); setAccId.add(acc.id); System.Test.startTest(); AccountProcessor.countContacts(setAccId); System.Test.stopTest(); Account acct = [SELECT Number_of_Contacts__c FROM Account WHERE id = :acc.id LIMIT 1]; System.assertEquals ( Integer.valueOf(acct.Number_of_Contacts__c) ,1); } }