Learnify_Rectify

Control Processes with Queueable Apex

Jun 23rd, 2024
7,554
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | Source Code | 0 0
  1. Control Processes with Queueable Apex from (module - Asynchronous Apex)
  2.  
  3. -------------------------------------------------
  4. SOURCE CODE1: AddPrimaryContact
  5.  
  6. public class AddPrimaryContact implements Queueable
  7. {
  8. private Contact c;
  9. private String state;
  10. public AddPrimaryContact(Contact c, String state)
  11. {
  12. this.c = c;
  13. this.state = state;
  14. }
  15. public void execute(QueueableContext context)
  16. {
  17. List<Account> ListAccount = [SELECT ID, Name ,(Select id,FirstName,LastName from contacts ) FROM ACCOUNT WHERE BillingState = :state LIMIT 200];
  18. List<Contact> lstContact = new List<Contact>();
  19. for (Account acc:ListAccount)
  20. {
  21. Contact cont = c.clone(false,false,false,false);
  22. cont.AccountId = acc.id;
  23. lstContact.add( cont );
  24. }
  25. if(lstContact.size() >0 )
  26. {
  27. insert lstContact;
  28. }} }
  29.  
  30. -------------------------------------------------
  31. SOURCE CODE2: AddPrimaryContactTest
  32.  
  33. @isTest
  34. public class AddPrimaryContactTest {
  35. @isTest static void TestList()
  36. {
  37. List<Account> Teste = new List <Account>();
  38. for(Integer i=0;i<50;i++)
  39. {
  40. Teste.add(new Account(BillingState = 'CA', name = 'Test'+i));
  41. }
  42. for(Integer j=0;j<50;j++)
  43. {
  44. Teste.add(new Account(BillingState = 'NY', name = 'Test'+j));
  45. }
  46. insert Teste;
  47. Contact co = new Contact();
  48. co.FirstName='demo';
  49. co.LastName ='demo';
  50. insert co;
  51. String state = 'CA';
  52. AddPrimaryContact apc = new AddPrimaryContact(co, state);
  53. Test.startTest();
  54. System.enqueueJob(apc);
  55. Test.stopTest();
  56.       } }
  57.  
  58.  
Add Comment
Please, Sign In to add comment