Advertisement
Guest User

ST_RoundAfterUpsert

a guest
May 30th, 2013
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.13 KB | None | 0 0
  1. public class ST_RoundAfterUpsert {
  2. public static void setLastTouched(List<FRLS_Round__c> updatedRounds){
  3. Map<Id,DateTime> mapAcctIdToLastTouched=new Map<Id,DateTime>();
  4. List<Account> accountObjs=new List<Account>();
  5. // Loop through the Rounds to get the unique Account Ids
  6. for(FRLS_Round__c cr : updatedRounds){
  7. if (cr.Account__c!=null){
  8. mapAcctIdToLastTouched.put(cr.Account__c,cr.LastModifiedDate);
  9. }
  10. }
  11. if (mapAcctIdToLastTouched.isEmpty()==False){
  12. // Get the Account objects so we can modify the Last_Touched__c field
  13. Try {
  14. accountObjs=[Select a.Last_Touched__c From Account a Where a.Id IN: mapAcctIdToLastTouched.keySet()];
  15. } catch (Exception e){
  16. // Do nothing
  17. }
  18. if (accountObjs.isEmpty()==False){
  19. // Loop through the Accounts and set the Last Touched field
  20. for(Account ca : accountObjs){
  21. ca.Last_Touched__c=mapAcctIdToLastTouched.get(ca.Id);
  22. }
  23. update accountObjs;
  24. }
  25. }
  26. }
  27.  
  28. public static void updateReferenceFields(List<FRLS_Round__c> updateRounds) {
  29. /*
  30. This sets fields on the round object that are meant to merge lead and contact fields
  31. for reporting on leads and contacts at the same time
  32.  
  33. Keep in mind, although this is in a class called RoundAfterUpsert, it actually
  34. happens BeforeUpsert...
  35. */
  36. Set<Id> related_contact_ids = new Set<Id>();
  37. Set<Id> related_lead_ids = new Set<Id>();
  38.  
  39. // get the ids for related leads/contacts you're going to query
  40. for(FRLS_Round__c round: updateRounds){
  41. if (round.Contact__c != null){
  42. related_contact_ids.add((Id) round.Contact__c);
  43. } else if(round.Lead__c != null){
  44. related_lead_ids.add((Id) round.Lead__c);
  45. }
  46. }
  47. // query for related leads/contacts
  48. List<Contact> related_contacts = [SELECT c.id, c.phone, c.title, c.createdDate, c.email, c.name, c.owner.name, c.AssistantName, c.account.name
  49. FROM Contact c WHERE c.id IN: related_contact_ids];
  50. List<Lead> related_leads = [SELECT l.id, l.phone, l.company, l.name, l.createdDate, l.email, l.PPM_Assistant__c, l.owner.name, l.title, l.LeadSource
  51. FROM Lead l WHERE l.id IN: related_lead_ids];
  52.  
  53. // put them in a dictionary for easier lookups by id
  54. Map<Id, SObject> related_lookup = new Map<Id, SObject>();
  55. for(SObject c : related_contacts) {
  56. related_lookup.put(c.Id, c);
  57. }
  58. for(SObject l : related_leads) {
  59. related_lookup.put(l.Id, l);
  60. }
  61.  
  62. // loop through one more time and update corresponding fields
  63. for(FRLS_Round__c round: updateRounds){
  64. if (round.Contact__c != null/* && round.Contact__c != ''*/){
  65. Contact rel = (Contact) related_lookup.get((Id) round.Contact__c);
  66. round.Rel_Phone_Number__c = rel.phone;
  67. round.Rel_Company__c = rel.Account.name;
  68. round.Rel_Title__c = rel.title;
  69. round.Rel_Owner__c = rel.owner.name;
  70. round.Rel_e_mail__c = rel.email;
  71. round.Rel_assistant__c = rel.AssistantName;
  72. round.Rel_Created_Date__c = rel.createdDate;
  73. } else if(round.Lead__c != null/* && round.Lead__c.id != ''*/){
  74. Lead rel = (Lead) related_lookup.get((Id) round.Lead__c);
  75. round.Rel_Phone_Number__c = rel.phone;
  76. round.Rel_Company__c = rel.Company;
  77. round.Rel_Title__c = rel.title;
  78. round.Rel_Owner__c = rel.owner.name;
  79. round.Rel_Lead_Source__c = rel.LeadSource;
  80. round.Rel_e_mail__c = rel.email;
  81. round.Rel_assistant__c = rel.PPM_Assistant__c;
  82. round.Rel_Created_Date__c = rel.createdDate;
  83. }
  84. }
  85.  
  86. }
  87.  
  88. // Test code for this class
  89. private static testMethod void testRoundAfterUpsert(){
  90. Account testAccount;
  91. Contact testContact;
  92. Lead testLead;
  93.  
  94. testAccount = new Account();
  95. testAccount = (Account) ST_Utilities.Populate_sObject(testAccount);
  96. insert testAccount;
  97. testContact = new Contact();
  98. testContact = (Contact) ST_Utilities.Populate_sObject(testContact);
  99. testContact.AccountId = testAccount.Id;
  100. insert testContact;
  101.  
  102. testLead = new Lead();
  103. testLead = (Lead)ST_Utilities.Populate_sObject((sObject)testLead);
  104. insert testLead;
  105. // Start the test
  106. Test.startTest();
  107. // Create a test Round record
  108. FRLS_Round__c testRound=new FRLS_Round__c();
  109. FRLS_Round__c testRoundAfter=new FRLS_Round__c();
  110.  
  111. testRound.Name='Open Round';
  112. testRound.Task__c='Callback';
  113. testRound.Contact__c=testContact.Id;
  114.  
  115. insert testRound;
  116.  
  117. testRoundAfter.Name='Closed Round';
  118. testRoundAfter.Task__c='Hot';
  119. testRoundAfter.Lead__c=testLead.Id;
  120. insert testRoundAfter;
  121.  
  122. // Make sure Last Touched was set on the Account
  123. testAccount=[Select a.Last_Touched__c From Account a Where a.Id=: testContact.AccountId];
  124. System.assertNotEquals(testAccount.Last_Touched__c,null);
  125. // Reset the Last Touched field to null
  126. testAccount.Last_Touched__c=null;
  127. update testAccount;
  128. // Change the round
  129. testRound.Task__c='Hot';
  130. testRound.Callback_Date__c=null;
  131. update testRound;
  132.  
  133. // Make sure Last Touched was set on the Account
  134. testAccount=[Select a.Last_Touched__c From Account a Where a.Id=: testContact.AccountId];
  135. System.assertNotEquals(testAccount.Last_Touched__c,null);
  136. // End the test
  137. Test.stopTest();
  138. }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement