Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.23 KB | None | 0 0
  1. public class contactActivityHandler {
  2.  
  3. public static void updateContact(List<Task> tks){ // Trigger.New comes as List here
  4. Set <Id> ContactIds = new Set<Id>();
  5. List <Contact> ContactList = new List<Contact>();
  6.  
  7. for(Task t :tks)
  8. {
  9. if(t.whoId!=null)
  10. {
  11. Schema.SObjectType tType= t.whoId.getSObjectType();
  12. if(tType == Contact.Schema.SObjectType)
  13. {
  14. ContactIds.add(t.WhoId);
  15. }
  16. }
  17. }
  18. //Querying the related Contacts based on whoId on Task
  19. Map<Id,Contact> ContactMap = new Map<Id,Contact>([select id, Last_Contact_Date__c,Name, Last_Activity_TypeOfInteraction__c, LastActivityDate, Last_Activity_Subject__c,Last_Activity_Date__c, Last_Activity_Name__c from Contact where id in:ContactIds]);
  20. for(Task t :tks)
  21. {
  22. If((!t.Subject.contains('DFP sent')))
  23. {
  24. for(Contact l : ContactMap.Values())
  25. {
  26. If(t.ActivityDate >=l.Last_Contact_Date__c)
  27. {
  28. l.Last_Activity_Subject__c = t.subject;
  29. l.Last_Activity_Date__c = t.ActivityDate;
  30. l.Last_Activity_Name__c = l.Name;
  31. l.Last_Activity_TypeOfInteraction__c = t.Type_of_Interaction__c;
  32. ContactList.add(l);
  33. }
  34. }
  35. }
  36. }
  37. // updating the Lead
  38. if(ContactList.size()>0)
  39. {
  40.  
  41. update ContactList;
  42. }
  43. }//End of Update Contact
  44.  
  45. public static void updateOldContact(List<Task> tks, List<Task>tksOld){ // Trigger.New comes as List here
  46.  
  47. Set<Id> ContactIds = new Set<Id>();
  48. Set<Id> OldContactIds = new Set<Id>(); //to get old contact ids
  49. List<Contact> ContactList = new List<Contact>();
  50. List<Contact> OldContactList = new List<Contact>();
  51. List<Contact> AllUpdateContactList = new List<Contact>();
  52.  
  53. for(Task t :tks) // looping through Ids and saves as Contacts Ids?
  54. {
  55. if(t.whoId!=null)
  56. {
  57. Schema.SObjectType tType= t.whoId.getSObjectType();
  58. if(tType == Contact.Schema.SObjectType)
  59. {
  60.  
  61. ContactIds.add(t.WhoId);
  62. }
  63. }
  64. }//End of First loop
  65.  
  66. for(Task ot :tksOld) // looping through Ids and saves as Contacts Ids?
  67. {
  68. if(ot.whoId!=null)
  69. {
  70. Schema.SObjectType otType= ot.whoId.getSObjectType();
  71. if(otType == Contact.Schema.SObjectType)
  72. {
  73.  
  74. OldContactIds.add(ot.WhoId); //Get old contact Id
  75. }
  76. }
  77. }//End of Second loop
  78.  
  79.  
  80. //Querying the related Contacts based on whoId on Task
  81. for(Id conold : OldContactIds){
  82. if(ContactIds.contains(conold)==false){ //Check old id with new id
  83. Map<Id,Contact> OldContactMap = new Map<Id,Contact>([select id, Last_Contact_Date__c,Name, Last_Activity_TypeOfInteraction__c, LastActivityDate, Last_Activity_Subject__c,Last_Activity_Date__c, Last_Activity_Name__c from Contact where id in:OldContactIds]);
  84. Map<Id,Contact> NewContactMap = new Map<Id,Contact>([select id,Last_Contact_Date__c,Name, Last_Activity_TypeOfInteraction__c, LastActivityDate, Last_Activity_Subject__c,Last_Activity_Date__c, Last_Activity_Name__c from Contact where id in:ContactIds]);
  85. List<Task> tskrecs = [select id,Subject,ActivityDate,Type_of_Interaction__c from task where WhoId =: ContactIds Order By lastmodifieddate DESC LIMIT 1];
  86. List<Task> Oldtskrecs = [select id,Subject,ActivityDate,Type_of_Interaction__c from task where WhoId =: OldContactIds Order By lastmodifieddate DESC LIMIT 1];
  87. for(Task t :tskrecs)
  88. {
  89. If((!t.Subject.contains('DFP sent')))
  90. {
  91. for(Contact l : OldContactMap.Values())
  92. {
  93. If(t.ActivityDate != null)
  94. {
  95. for(Contact nl : NewContactMap.values()){
  96.  
  97. nl.Last_Activity_Subject__c= t.subject;
  98. nl.Last_Activity_Date__c = t.ActivityDate;
  99. nl.Last_Activity_Name__c = l.Name;
  100. nl.Last_Activity_TypeOfInteraction__c = t.Type_of_Interaction__c;
  101. AllUpdateContactList.add(nl);
  102. }
  103. }
  104. }
  105. }
  106. }
  107. for(Task t :Oldtskrecs){
  108. If((!t.Subject.contains('DFP sent')))
  109. {
  110. for(Contact l : OldContactMap.Values())
  111. {
  112. If(t.ActivityDate != null)
  113. {
  114. l.Last_Activity_Subject__c = t.subject;
  115. l.Last_Activity_Date__c = t.ActivityDate;
  116. l.Last_Activity_Name__c = l.Name;
  117. l.Last_Activity_TypeOfInteraction__c = t.Type_of_Interaction__c;
  118. AllUpdateContactList.add(l);
  119. }
  120.  
  121. }
  122. }
  123.  
  124. }
  125. update AllUpdateContactList;
  126. }else if(ContactIds.contains(conold)== true ){
  127. updateContact(tks);
  128. }
  129. }
  130. }//End of Update Old Contact
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement