You need to sign in to do that
Don't have an account?
John Neilan
Update Opportunity When Task Deleted
Hello,
I have a trigger that fires when a task is inserted or updated. When it is, it marks a checkbox on the Opportunity as TRUE. What I would now like to do is add logic that will mark the field as FALSE if that task is deleted, but I'm not sure how to do this. Can anyone help based on the trigger and class below?
Class:
Trigger:
I have a trigger that fires when a task is inserted or updated. When it is, it marks a checkbox on the Opportunity as TRUE. What I would now like to do is add logic that will mark the field as FALSE if that task is deleted, but I'm not sure how to do this. Can anyone help based on the trigger and class below?
Class:
public class ClassRenewalTasks { public void renewalTasks(List<Task> checkTasks) { // set up lists you will need List<Opportunity> linkedOpps = new List<Opportunity>(); Map<Id, Task> taskMap = new Map<Id, Task>(); // go through the list of tasks that were inserted for (Task t: checkTasks) { // if they are related to a contact, add the contact id (whoID) and their values to a map if (t.WhatId != null && t.Status == 'Completed' && (t.Subject == 'User Training Complete'|| t.Subject == 'Initial Program Confirmation'|| t.Subject == 'Renewal Package Review')){ taskMap.put(t.WhatId, t); } } // if the map isnt empty // *** saying !taskMap.isEmpty() costs much less than using taskMap.size()>0 *** system.debug('taskMap = '+taskMap); if (taskMap.size() > 0) { // get all of the contacts related to the tasks linkedOpps = [SELECT Id, RP_User_Training__c, RP_Initial_Program_Developed__c, RP_Recommendation_Review_Call__c FROM Opportunity WHERE Id IN: taskMap.keySet()]; // go through the list for each contact for (Opportunity c: linkedOpps){ for (Task task2:[SELECT WhatId, Subject FROM Task WHERE WhatId =: c.Id]){ IF(task2.Subject == 'User Training Complete'){ c.RP_User_Training__c = TRUE; } IF(task2.Subject == 'Initial Program Confirmation'){ c.RP_Initial_Program_Developed__c = TRUE; } IF(task2.Subject == 'Renewal Package Review'){ c.RP_Recommendation_Review_Call__c = TRUE; } } } // if the list of cons isnt empty, update them system.debug('linkedOpps = '+linkedOpps); if (linkedOpps.size() > 0) { update linkedOpps; } } } }
Trigger:
trigger MainTriggerTask on Task (after insert, after update, after delete) { IF(Trigger.IsAfter){ IF(Trigger.IsInsert){ if(checkTaskRecursive.runAfterInsertOnce()){ ClassRenewalTasks updater6 = new ClassRenewalTasks(); updater6.renewalTasks(Trigger.new); } } IF(Trigger.IsUpdate){ if(checkTaskRecursive.runAfterUpdateOnce()){ ClassRenewalTasks updater3 = new ClassRenewalTasks(); updater3.renewalTasks(Trigger.new); } } }
Below is updated trigger and class.
Best Regards,
Mithun.
All Answers
Below is updated trigger and class.
Best Regards,
Mithun.