You need to sign in to do that
Don't have an account?
Warren Wade
Trouble with Apex Trigger
I'm incredibly new to all of this so I appreciate any help you're able to offer.
I've got a Task Trigger that is firing correctly for just general tasks; however, I want to add a filter to the type of tasks that update the related whoId. I basically only want the whoId to be updated if the task is complete and contains "Inbound" which is contained in multiple values in a single picklist. The "Inbound" string in Step A is there because I'm not sure how to filter by the actual field.
I've got a Task Trigger that is firing correctly for just general tasks; however, I want to add a filter to the type of tasks that update the related whoId. I basically only want the whoId to be updated if the task is complete and contains "Inbound" which is contained in multiple values in a single picklist. The "Inbound" string in Step A is there because I'm not sure how to filter by the actual field.
trigger LastActivityUpdate on Task (after insert, after update) { //Step A: Add the Inbound Set Set<String> Inbound = new Set<String>{'Inbound Letter', 'Inbound Email','Inbound Web', 'Inbound Call'}; //Step 1: Create a set of Contact IDs from the task Set<String> whoIds = new Set<String>(); //for these tasks, pull in the whoID for (Task t : Trigger.new){ // This "Done" criteria was throwing some system exceptions so I've just got it noted. // if (t.Done__c = TRUE){ whoIds.add(t.WhoId); // } } //Step 2: list that Contact and their "Last Inbound Activity" field List<Contact> cons = [SELECT Id, Last_Inbound_Activity__c FROM Contact WHERE Id =: whoIds]; //Step 3: iterate over tasks and create map of tasks by WhoId Map<String, Task> taskMap = new Map<String, Task>(); for (Task t : Trigger.new){ taskMap.put(t.whoID, t); } //Step 4: iterate over Contacts, retrieve fields and set with ActivityDate for (Contact c : cons) { if (taskMap.containsKey(c.Id)) c.Last_Inbound_Activity__c = taskMap.get(c.Id).ActivityDate; } //Lastly, update the contact update cons; }
At line no. 10, you can do the following check
if(t.Status == 'Completed' && Inbound.contains(t.customfield))
whoId.add(t.whoid);