2

I have this trigger on contact that I would run only if Task is inserted and updated. I was thinking to create a checkbox on contact and set it true using may be using process builder that would trigger the contact trigger. Am I on the right path or is there a better way to do this?

Here's the code:

trigger contactTrigger on Contact (after insert, after update) {
   if(contactHandlerClass.contactRecursion)
    return;
  contactHandlerClass.contactRecursion = true;
        contactHandlerClass.getRecentTaskInformation(Trigger.new);

}


public class contactHandlerClass {
     public static boolean contactRecursion =false; // to avoid the "maximum trigger depth exceeded" error


    public static void getRecentTaskInformation(List<Contact> contacts) {

        System.debug('print the list' + contacts);

        List<Contact> contactsWithLastTasks = [
            SELECT Id, Last_Activity_Subject__c, Last_Activity_Date__c,Contact_Task_Date__c,    Most_Recent_Contact_Task_Subject__c,
                Last_Activity_Name__c , Last_Activity_Assigned_to__c ,
                Last_Activity_TypeOfInteraction__c , Name, (
                    SELECT Subject, ActivityDate, Owner.Name, 
                        Type_of_Interaction__c
                    FROM Tasks
                    ORDER BY ActivityDate DESC 
                    LIMIT 1
                )
            FROM Contact WHERE Id IN :contacts
        ];

        for (Contact c : contactsWithLastTasks) {
            if (!c.tasks.isEmpty()) {
                Task lastTask = c.tasks[0];
                System.debug('print Tasks' + lastTask);

                c.Most_Recent_Contact_Task_Subject__c = lastTask.Subject;
                c.Contact_Task_Date__c = lastTask.ActivityDate;

                System.debug('Print contacts list one by one?'+ contactsWithLastTasks);
                System.debug('Field one?'+ c.Most_Recent_Contact_Task_Subject__c);
                System.debug('Field two?'+ c.Contact_Task_Date__c);

            }
        }
        If(!contactsWithLastTasks.isEmpty()){
        Update contactsWithLastTasks;
        }
    }

}

enter image description here

My proces builder on "Task object" to update Contact field "TestTriggerCheckbox" I did create same thing where to set to "True" from "False" and vice versa. Thank you for the help! enter image description here enter image description here

8
  • you don't need a custom field on Contact; merely updating the Contact by referencing its ID will cause the contact trigger to fire. But you are correct that some DML on the parent Contact must be performed
    – cropredy
    Sep 29, 2019 at 5:26
  • Ok, but this would only trigger if contact is updated. I am trying to find a way to trigger this so when the task is inserted or updated, it will make this trigger to run.
    – mike
    Sep 30, 2019 at 13:11
  • I'm just saying that the PB on Task merely needs to cause a DML on Contact for trigger to execute. Of course, PB on Task could start a flow that did Contact update and you could dispense with trigger.
    – cropredy
    Sep 30, 2019 at 13:22
  • ok so going back to your earlier comment. You said, "merely updating the Contact by referencing its ID " I was little confused and I wonder how would I do that?
    – mike
    Sep 30, 2019 at 14:59
  • Task.WhoId references the Contact
    – cropredy
    Sep 30, 2019 at 15:44

1 Answer 1

1

Do you not have tasks that are also associated to leads? It seems with your PB approach you'd always be assuming it's on a contact?

Regardless, you could just create an apex trigger on task and then check the sObject of the whoID. If it's an ID that represents a contact, then you can directly pass the info to the contact code you already have. This leaves open room to add different logic if it's a lead as well.

String sObjectType = taskRec.WhoId.getSObjectType().getDescribe().getName();

if(sObjectType == 'Contact'){
  // pass task info and contact info to code you already have
}
1
  • Thank you I will try it out..
    – mike
    Oct 11, 2019 at 17:06

You must log in to answer this question.

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