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;
}
}
}
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!