Created
March 9, 2016 08:01
-
-
Save tyoshikawa1106/06351cc6a3e304abe73d to your computer and use it in GitHub Desktop.
@futureのサンプル
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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