0

This trigger inserts and updates contact fields fine for the first contact but when there are multiple contacts in Task, the task does not update contact fields for the rest of the contacts. What am I doing wrong? Thank you in advance!

if (Trigger.isafter && Trigger.isInsert){ 
              ActivityHandlerClass.insertContact(Trigger.New);
     System.debug('I RAN INSERT');

     } 



public class ActivityHandlerClass {
        public static void insertContact(List<Task> tks){ // Trigger.New comes as List here
        Set<Id> ContactIds = new Set<Id>();
        List<Contact> ContactList = new List<Contact>();

        List<Task> taskContacts = [SELECT Id, Subject, (SELECT RelationId, Relation.Name from TaskRelations) FROM Task WHERE id in:tks];
        System.debug('Print Task Contacts: ' + taskContacts);

        for(Task ti: taskContacts)
        {
            for(TaskRelation tRel: ti.TaskRelations)
            {
               System.debug('Print Task Contacts: ' + tRel);
                System.debug('Print Task Contact Size: ' + ti.TaskRelations.Size());
                If(ti.TaskRelations.Size() >0){
                       ContactIds.add(tRel.RelationId);
                  System.debug('Print added to ContactIds: ' + ContactIds);

                }
            }

        }
                List<Contact> relatedContacts = [select id,Last_Contact_Date__c,LastActivityDate, Last_Activity_Subject__c from Contact where id in:ContactIds];
                System.debug('Print RelatedContacts :' + relatedContacts);

        for(Task t :tks)
        {
                for(Contact con : relatedContacts)
                {  
                  If(t.ActivityDate >=con.Last_Contact_Date__c)
                    {
                    con.Last_Activity_Subject__c = t.subject;
                    con.Last_Activity_Date__c = t.ActivityDate;

                    ContactList.add(con);
                         System.debug('Printining that ADDDDDDDDDDDDDDDDDDDDD   ran');  
                         System.debug('ContactList is being printed:' + ContactList); 
                         System.debug('ContactList ID is being printed:***********' + con.id); 
                    } 
                 } 
            System.debug('Update ContactList***************');
        } 

           If(ContactList.size()>0)
           {   
            upsert ContactList; 
           }
  } // End of Insert Method

Below image shows that it updates contact field fine for the first contact but rest gets ignored.

enter image description here

Here's updated images with Robert Solution enter image description here

enter image description here

1 Answer 1

1

from your description and the code you posted I think you want to achieve that if a task is getting inserted or updated, the activity date should be written on all related contacts.

This would be my approach

public static void updateContact(List<Task> tks) {
    Map<Id, Contact> affectedContacts = new Map<Id, Contact>([SELECT Id, Last_Contact_Date__c 
                                                              FROM Contact 
                                                              WHERE Id IN (SELECT RelationId 
                                                                           FROM TaskRelation 
                                                                           WHERE TaskId IN :tks)]);
    Map<Id, Contact> contactsToUpdate = new Map<Id, Contact>();

    for(TaskRelation tr : [SELECT Task.ActivityDate, Task.Subject, RelationId
                           FROM TaskRelation
                           WHERE TaskId IN :tks]) {

        Contact c = affectedContacts.get(tr.RelationId);

        if(c != null && tr.Task.ActivityDate >= c.Last_Contact_Date__c) {
            c.Last_Activity_Subject__c = tr.Task.Subject;
            c.Last_Activity_Date__c = tr.Task.ActivityDate;

            contactsToUpdate.put(c.Id, c);
        }
    }

    update contactsToUpdate.values();
}

However you should think about the possibility that two or more tasks are updated that link to the same contact. You would have to set rule for how to behave with conflicting activity dates on the same contact.

Hope that helps.

6
  • 2
    nice and clean; pro tip: if( !contactsToUpdate.isEmpty()) { is unnecessary, DML on empty lists never burns DML statement.
    – cropredy
    Commented Sep 25, 2019 at 23:09
  • Thank you, Robert. I will test it out first thing in the morning. I have zero clues on how to handle two or more tasks are updated on the same contact. I guess I need to think about that some more on that issue later.
    – mike
    Commented Sep 26, 2019 at 2:24
  • You would have to make a general rule like "The newest Task wins" or "the task with the highest activity date wins". Then you can include this check in the if statement by comparing it against what is already in the "contactsToUpdate" map. Mark this answer as accepted if it solved your problem ;-) @cropredy thx for the tip I will update my snippet. Commented Sep 26, 2019 at 12:32
  • Robert, I tested this trigger and It did not work. It is doing the same thing. It inserts for the first contact where I am writing task but rest do not get updated. I have updated the images on top.
    – mike
    Commented Sep 26, 2019 at 20:36
  • currently your code does not insert any contact but just updates the existing contacts to that task. However it might be that the Last_Contact_Date__c values of those contacts are not older than the ActivityDate of the Task. Commented Sep 26, 2019 at 21:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .