+ Start a Discussion
John NeilanJohn Neilan 

Update Opportunity When Task Created on Account

Hello,

I have a trigger that is meant to update custom field on an Opportunity when specific tasks are created on an Account that is also linked to the Opportunity.  The trigger below saves and does not give any errors, however, nothing happens when a task is created.  Can anyone help me figure out why?  Thanks.

Trigger:
trigger MainTriggerTask on Task (after insert, after update, before delete) {
    
    IF(Trigger.IsAfter){
        IF(Trigger.IsInsert){
                ClassRenewalTasks updater6 = new ClassRenewalTasks();
                updater6.renewalTasks(Trigger.new);
        }
        
        IF(Trigger.IsUpdate){
            
                ClassRenewalTasks updater3 = new ClassRenewalTasks();
                updater3.renewalTasks(Trigger.new);
                
        }
    }

    ELSE IF(Trigger.IsBefore){
        IF(Trigger.IsDelete){
                ClassRenewalTasks deleteTsk = new ClassRenewalTasks();
                deleteTsk.deleteTasks(Trigger.old);

            }
    }
}

Class:
public class ClassRenewalTasks {

    public void deleteTasks(List<Task> delTasks) {
        List<Opportunity> linkedOpps = new List<Opportunity>();
        Map<Id, Task> taskMap = new Map<Id, Task>();
 
        for (Task t: delTasks) {
                taskMap.put(t.WhatId, t);
        }
        if (taskMap.size() > 0)
        {
            linkedOpps = [SELECT Id,Account.Id, Effective_Date__c, Next_Renewal_Date__c, RP_User_Training__c,
                                RP_Initial_Program_Developed__c
                            FROM Opportunity
                            WHERE Account.Id IN: taskMap.keySet()];

            for (Opportunity opp1: linkedOpps){
                for (Task delTask:[SELECT WhatId, Subject, Status, ActivityDate
                                FROM Task
                                WHERE WhatId =: opp1.Account.Id AND Subject =: 'Completed' AND ActivityDate >=: opp1.Effective_Date__c AND ActivityDate <=: opp1.Next_Renewal_Date__c]){
                    IF(delTask.Subject == 'User Training Complete'){
                        opp1.RP_User_Training__c = FALSE;
                    }
                    IF(delTask.Subject == 'Initial Program Confirmation'){
                        opp1.RP_Initial_Program_Developed__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.Subject == 'User Training Complete' ||
                                    t.Subject == 'Initial Program Confirmation')){
                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 Account.Id, Effective_Date__c, Next_Renewal_Date__c, RP_User_Training__c,
                                RP_Initial_Program_Developed__c
                            FROM Opportunity 
                            WHERE Account.Id IN: taskMap.keySet()];
            // go through the list for each contact
            for (Opportunity c: linkedOpps){
                for (Task task2:[SELECT WhatId, Subject, Status, ActivityDate
                                FROM Task
                                WHERE WhatId =: c.Account.Id AND ActivityDate >=: c.Effective_Date__c AND ActivityDate <=: c.Next_Renewal_Date__c]){
                    IF(task2.Subject == 'User Training Complete' && task2.Status == 'Completed'){
                        c.RP_User_Training__c = TRUE;
                    }
                    IF(task2.Subject == 'Initial Program Confirmation' && task2.Status == 'Completed'){
                        c.RP_Initial_Program_Developed__c = TRUE;
                    }
                    IF(task2.Subject == 'User Training Complete' && task2.Status <> 'Completed'){
                        c.RP_User_Training__c = FALSE;
                    }
                    IF(task2.Subject == 'Initial Program Confirmation' && task2.Status <> 'Completed'){
                        c.RP_Initial_Program_Developed__c = FALSE;
                    }
                }
            }

            // if the list of cons isnt empty, update them
            system.debug('linkedOpps = '+linkedOpps);
            if (linkedOpps.size() > 0)
            {
            update linkedOpps;
            }
        }
    }
}


 
Best Answer chosen by John Neilan
John NeilanJohn Neilan
Thanks.  I figured it out.  One of the fields I was using as a criteria was not being populated as it should have.  I changed by Next_Renewal_Date__c field to a formula field and everything now works.  Thanks for your assistance!

All Answers

YuchenYuchen
Your code looks fine to me. So what does the taskMap and linkedOpps print in the debug logs? Also, does the following query return any value:

SELECT WhatId, Subject, Status, ActivityDateFROM Task WHERE WhatId =: c.Account.Id AND ActivityDate >=: c.Effective_Date__c AND ActivityDate <=: c.Next_Renewal_Date__c
John NeilanJohn Neilan
Thanks.  I figured it out.  One of the fields I was using as a criteria was not being populated as it should have.  I changed by Next_Renewal_Date__c field to a formula field and everything now works.  Thanks for your assistance!
This was selected as the best answer