+ Start a Discussion
John NeilanJohn 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:
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);
            }
            
        }
        
}

 
Best Answer chosen by John Neilan
MithunPMithunP
Hi John,

Below is updated trigger and class.
trigger MainTriggerTask on Task (after insert, after update, after delete, before 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);
            }
        }
        
    }
    else if(Trigger.IsBefore){
        IF(Trigger.IsDelete){
            if(checkTaskRecursive.runAfterUpdateOnce()){
                ClassRenewalTasks deleteTsk = new ClassRenewalTasks();
                deleteTsk.deleteTasks(Trigger.old);
            }
        }
    }
        
}


 
public class ClassRenewalTasks {
    public void deleteTasks(List<Task> checkTasks) {
        List<Opportunity> linkedOpps = new List<Opportunity>();
        Map<Id, Task> taskMap = new Map<Id, Task>();

        for (Task t: checkTasks) {
                taskMap.put(t.WhatId, t);
        }
        if (taskMap.size() > 0)
        {
            linkedOpps = [SELECT Id, RP_User_Training__c,
                                RP_Initial_Program_Developed__c,
                                RP_Recommendation_Review_Call__c
                            FROM Opportunity 
                            WHERE Id IN: taskMap.keySet()];
            for (Opportunity c: linkedOpps){
                   c.RP_User_Training__c = False;
                   c.RP_Initial_Program_Developed__c = False;
                   c.RP_Recommendation_Review_Call__c = False;
                  
            }
        }

            // if the list of cons isnt empty, update them
            system.debug('linkedOpps = '+linkedOpps);
            if (linkedOpps.size() > 0)
            {
            update linkedOpps;
            }
        
    }
    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;
            }
        }
    }
}
Best Regards,
Mithun.
 

All Answers

Crystal Rochlitz 4Crystal Rochlitz 4
First as a heads up, you have a query in a for loop on line 30. Don't want you to max out your limits. Best practices and all. To get to your question, I believe you would need a trigger on task deletion, a query against the WhatId and the isDeleted field (new and old values). If wasn't deleted, now is deleted, get the corresponding WhatId from the query map and update the field(s) as needed. You can always pre-qualify the tasks that pass through the trigger to only run the logic on the ones that need it. I hope that's enough to get you started. If you need more info/help please reply.
MithunPMithunP
Hi John,

Below is updated trigger and class.
trigger MainTriggerTask on Task (after insert, after update, after delete, before 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);
            }
        }
        
    }
    else if(Trigger.IsBefore){
        IF(Trigger.IsDelete){
            if(checkTaskRecursive.runAfterUpdateOnce()){
                ClassRenewalTasks deleteTsk = new ClassRenewalTasks();
                deleteTsk.deleteTasks(Trigger.old);
            }
        }
    }
        
}


 
public class ClassRenewalTasks {
    public void deleteTasks(List<Task> checkTasks) {
        List<Opportunity> linkedOpps = new List<Opportunity>();
        Map<Id, Task> taskMap = new Map<Id, Task>();

        for (Task t: checkTasks) {
                taskMap.put(t.WhatId, t);
        }
        if (taskMap.size() > 0)
        {
            linkedOpps = [SELECT Id, RP_User_Training__c,
                                RP_Initial_Program_Developed__c,
                                RP_Recommendation_Review_Call__c
                            FROM Opportunity 
                            WHERE Id IN: taskMap.keySet()];
            for (Opportunity c: linkedOpps){
                   c.RP_User_Training__c = False;
                   c.RP_Initial_Program_Developed__c = False;
                   c.RP_Recommendation_Review_Call__c = False;
                  
            }
        }

            // if the list of cons isnt empty, update them
            system.debug('linkedOpps = '+linkedOpps);
            if (linkedOpps.size() > 0)
            {
            update linkedOpps;
            }
        
    }
    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;
            }
        }
    }
}
Best Regards,
Mithun.
 
This was selected as the best answer
John NeilanJohn Neilan
Thanks Mithun, that did it!