Advertisement
Learnify_Rectify

Use Future Methods

Jun 22nd, 2024
9,798
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | Source Code | 0 0
  1. Use Future Methods from (module - Asynchronous Apex)
  2.  
  3. -------------------------------------------------
  4. SOURCE CODE1:
  5.  
  6. public class AccountProcessor {
  7. @future
  8. public static void countContacts(List<Id> accountIds){
  9. List<Account> accounts = [Select Id, Name from Account Where Id IN : accountIds];
  10. List<Account> updatedAccounts = new List<Account>();
  11. for(Account account : accounts){
  12. account.Number_of_Contacts__c = [Select count() from Contact Where AccountId =: account.Id];
  13. System.debug('No Of Contacts = ' + account.Number_of_Contacts__c);
  14. updatedAccounts.add(account);
  15. }
  16. update updatedAccounts;
  17.     } }
  18.  
  19. -------------------------------------------------
  20. Source CODE2:
  21.  
  22. @isTest
  23. public class AccountProcessorTest {
  24. @isTest
  25. public static void testNoOfContacts(){
  26. Account a = new Account();
  27. a.Name = 'Test Account';
  28. Insert a;
  29. Contact c = new Contact();
  30. c.FirstName = 'Bob';
  31. c.LastName = 'Willie';
  32. c.AccountId = a.Id;
  33. Contact c2 = new Contact();
  34. c2.FirstName = 'Tom';
  35. c2.LastName = 'Cruise';
  36. c2.AccountId = a.Id;
  37. List<Id> acctIds = new List<Id>();
  38. acctIds.add(a.Id);
  39. Test.startTest();
  40. AccountProcessor.countContacts(acctIds);
  41. Test.stopTest();
  42.     } }
  43.  
  44.  
  45.  
  46.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement