Skip to content

Instantly share code, notes, and snippets.

@tyoshikawa1106
Created March 9, 2016 08:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyoshikawa1106/06351cc6a3e304abe73d to your computer and use it in GitHub Desktop.
Save tyoshikawa1106/06351cc6a3e304abe73d to your computer and use it in GitHub Desktop.
@futureのサンプル
public class AccountProcessor {
@future
public static void countContacts(List<Id> accountIds) {
List<Account> accounts = [SELECT Id,(SELECT Id FROM Contacts) FROM Account WHERE Id IN: accountIds];
for (Account a : accounts) {
a.Number_of_Contacts__c = a.Contacts.size();
}
if (!accounts.isEmpty()) {
update accounts;
}
}
}
@isTest
private class AccountProcessorTest {
private static User testAdminUser = new User(Id = UserInfo.getUserId());
static testMethod void countContactsTest() {
System.runAs(testAdminUser) {
Account account = new Account(Name = 'Sample');
insert account;
Contact contact = new Contact(LastName = 'Yoshikawa', AccountId = account.Id);
insert contact;
List<Id> accountIds = new List<Id>();
accountIds.add(account.Id);
Test.startTest();
AccountProcessor.countContacts(accountIds);
Test.stopTest();
Account resultAccount = [SELECT Number_of_Contacts__c FROM Account WHERE Id =: account.Id LIMIT 1];
System.assertEquals(resultAccount.Number_of_Contacts__c, 1);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment