Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Use Future Methods from (module - Asynchronous Apex)
- -------------------------------------------------
- SOURCE CODE1:
- public class AccountProcessor {
- @future
- public static void countContacts(List<Id> accountIds){
- List<Account> accounts = [Select Id, Name from Account Where Id IN : accountIds];
- List<Account> updatedAccounts = new List<Account>();
- for(Account account : accounts){
- account.Number_of_Contacts__c = [Select count() from Contact Where AccountId =: account.Id];
- System.debug('No Of Contacts = ' + account.Number_of_Contacts__c);
- updatedAccounts.add(account);
- }
- update updatedAccounts;
- } }
- -------------------------------------------------
- Source CODE2:
- @isTest
- public class AccountProcessorTest {
- @isTest
- public static void testNoOfContacts(){
- Account a = new Account();
- a.Name = 'Test Account';
- Insert a;
- Contact c = new Contact();
- c.FirstName = 'Bob';
- c.LastName = 'Willie';
- c.AccountId = a.Id;
- Contact c2 = new Contact();
- c2.FirstName = 'Tom';
- c2.LastName = 'Cruise';
- c2.AccountId = a.Id;
- List<Id> acctIds = new List<Id>();
- acctIds.add(a.Id);
- Test.startTest();
- AccountProcessor.countContacts(acctIds);
- Test.stopTest();
- } }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement