Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class contactActivityHandler {
- public static void updateContact(List<Task> tks){ // Trigger.New comes as List here
- Set <Id> ContactIds = new Set<Id>();
- List <Contact> ContactList = new List<Contact>();
- for(Task t :tks)
- {
- if(t.whoId!=null)
- {
- Schema.SObjectType tType= t.whoId.getSObjectType();
- if(tType == Contact.Schema.SObjectType)
- {
- ContactIds.add(t.WhoId);
- }
- }
- }
- //Querying the related Contacts based on whoId on Task
- 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]);
- for(Task t :tks)
- {
- If((!t.Subject.contains('DFP sent')))
- {
- for(Contact l : ContactMap.Values())
- {
- If(t.ActivityDate >=l.Last_Contact_Date__c)
- {
- l.Last_Activity_Subject__c = t.subject;
- l.Last_Activity_Date__c = t.ActivityDate;
- l.Last_Activity_Name__c = l.Name;
- l.Last_Activity_TypeOfInteraction__c = t.Type_of_Interaction__c;
- ContactList.add(l);
- }
- }
- }
- }
- // updating the Lead
- if(ContactList.size()>0)
- {
- update ContactList;
- }
- }//End of Update Contact
- public static void updateOldContact(List<Task> tks, List<Task>tksOld){ // Trigger.New comes as List here
- Set<Id> ContactIds = new Set<Id>();
- Set<Id> OldContactIds = new Set<Id>(); //to get old contact ids
- List<Contact> ContactList = new List<Contact>();
- List<Contact> OldContactList = new List<Contact>();
- List<Contact> AllUpdateContactList = new List<Contact>();
- for(Task t :tks) // looping through Ids and saves as Contacts Ids?
- {
- if(t.whoId!=null)
- {
- Schema.SObjectType tType= t.whoId.getSObjectType();
- if(tType == Contact.Schema.SObjectType)
- {
- ContactIds.add(t.WhoId);
- }
- }
- }//End of First loop
- for(Task ot :tksOld) // looping through Ids and saves as Contacts Ids?
- {
- if(ot.whoId!=null)
- {
- Schema.SObjectType otType= ot.whoId.getSObjectType();
- if(otType == Contact.Schema.SObjectType)
- {
- OldContactIds.add(ot.WhoId); //Get old contact Id
- }
- }
- }//End of Second loop
- //Querying the related Contacts based on whoId on Task
- for(Id conold : OldContactIds){
- if(ContactIds.contains(conold)==false){ //Check old id with new id
- 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]);
- 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]);
- List<Task> tskrecs = [select id,Subject,ActivityDate,Type_of_Interaction__c from task where WhoId =: ContactIds Order By lastmodifieddate DESC LIMIT 1];
- List<Task> Oldtskrecs = [select id,Subject,ActivityDate,Type_of_Interaction__c from task where WhoId =: OldContactIds Order By lastmodifieddate DESC LIMIT 1];
- for(Task t :tskrecs)
- {
- If((!t.Subject.contains('DFP sent')))
- {
- for(Contact l : OldContactMap.Values())
- {
- If(t.ActivityDate != null)
- {
- for(Contact nl : NewContactMap.values()){
- nl.Last_Activity_Subject__c= t.subject;
- nl.Last_Activity_Date__c = t.ActivityDate;
- nl.Last_Activity_Name__c = l.Name;
- nl.Last_Activity_TypeOfInteraction__c = t.Type_of_Interaction__c;
- AllUpdateContactList.add(nl);
- }
- }
- }
- }
- }
- for(Task t :Oldtskrecs){
- If((!t.Subject.contains('DFP sent')))
- {
- for(Contact l : OldContactMap.Values())
- {
- If(t.ActivityDate != null)
- {
- l.Last_Activity_Subject__c = t.subject;
- l.Last_Activity_Date__c = t.ActivityDate;
- l.Last_Activity_Name__c = l.Name;
- l.Last_Activity_TypeOfInteraction__c = t.Type_of_Interaction__c;
- AllUpdateContactList.add(l);
- }
- }
- }
- }
- update AllUpdateContactList;
- }else if(ContactIds.contains(conold)== true ){
- updateContact(tks);
- }
- }
- }//End of Update Old Contact
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement