Skip to main content
added 204 characters in body
Source Link
mike
  • 71
  • 7

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

enter image description here

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

enter image description here

added 4 characters in body
Source Link
mike
  • 71
  • 7

[![enter image description here][1]][1]

Below image shows that it updates contact field fine for the first contact but rest gets ignored. [1]: https://i.sstatic.net/k7nQN.png

enter image description here

[![enter image description here][1]][1]

Below image shows that it updates contact field fine for the first contact but rest gets ignored. [1]: https://i.sstatic.net/k7nQN.png

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

enter image description here

Source Link
mike
  • 71
  • 7

Why this trigger won't insert task for all the contacts?

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

[![enter image description here][1]][1]

Below image shows that it updates contact field fine for the first contact but rest gets ignored. [1]: https://i.sstatic.net/k7nQN.png