+ Start a Discussion
John NeilanJohn Neilan 

Activity Count on Opportunity

Hello,

The trigger below fires when a Task is Inserted, Updated, or Deleted.  It looks at the Account the Task is associated with and matches it to any Open Opportunities associated to the same Account.  It then takes the count of tasks and writes it to a custom field on the Opportunity.  All works fine when tasks are inserted, deleted, or undeleted.  However, when I move a task from a Status of Closed to something else, the count should decrease, however, it does not.  I don't see any reason why it would not decrease.  Can anyone help me figure it out?  Thanks.

Trigger:
trigger MainTriggerTask on Task (after insert, after update, after delete, before delete, after undelete) {
    
    IF(Trigger.IsAfter){
        IF(Trigger.IsInsert){
                ClassRenewalTaskCount updater7 = new ClassRenewalTaskCount();
                updater7.taskCountInsert(Trigger.new);
            }
        }
        
        IF(Trigger.IsUpdate){
                ClassRenewalTaskCount updater4 = new ClassRenewalTaskCount();
                updater4.taskCountUpdate(Trigger.new,Trigger.old);
        }
        
        IF(Trigger.IsDelete){
                ClassRenewalTaskCount updater2 = new ClassRenewalTaskCount();
                updater2.taskCountDel(Trigger.old);
        }

        IF(Trigger.IsUndelete){
                ClassRenewalTaskCount updater2 = new ClassRenewalTaskCount();
                updater2.taskCountInsert(Trigger.new);
        }
    }
}


Class:
public class ClassRenewalTaskCount {

    Set<ID> OppIds = new Set<ID>();

        String acctPrefix = Account.SObjectType.getDescribe().getKeyPrefix();

    public void taskCountInsert(List<Task> checkTasks) {

        for (Task t : checkTasks) {
                if (t.WhatId != null && t.Status == 'Completed' && t.RP_Prior_60__c <= t.ActivityDate && string.valueof(t.WhatId).startsWith(acctPrefix) ) {
                    OppIds.add(t.WhatId);              
                }
        }
        if (OppIds.size() > 0){

            List<Account> acctsWithTasks = [SELECT Id,(SELECT Id FROM Tasks)
                                            FROM Account
                                            WHERE Id IN : OppIds];

                List<Opportunity> oppsUpdatable = new List<Opportunity>();

                    for(Account acct : acctsWithTasks){
                        for(Opportunity L : [SELECT Id,Account.Id,RP_Customer_Engagement__c,IsWon
                                            FROM Opportunity
                                            WHERE Account.Id =: acct.Id AND IsWon=FALSE]){
                            
                            L.RP_Customer_Engagement__c = acct.Tasks.size();
                        oppsUpdatable.add(L);
                        }
                    }

                if(oppsUpdatable.size()>0){
                update oppsUpdatable;
                }
        }
    }
 
    public void taskCountUpdate(List<Task> checkTasks, List<Task> oldTasks) {

        for (Task t : checkTasks) {
            for (Task prev : oldTasks){
                if (t.WhatId != null && t.Status == 'Completed' && t.RP_Prior_60__c <= t.ActivityDate && string.valueof(t.WhatId).startsWith(acctPrefix) ) {
                    OppIds.add(t.WhatId);              
                }
            }
        }

        if (OppIds.size() > 0){

            List<Account> acctsWithTasks = [SELECT Id,(SELECT Id FROM Tasks)
                                            FROM Account
                                            WHERE Id IN : OppIds];

                List<Opportunity> oppsUpdatable = new List<Opportunity>();

                    for(Account acct : acctsWithTasks){
                        for(Opportunity L : [SELECT Id,Account.Id,RP_Customer_Engagement__c,IsWon
                                            FROM Opportunity
                                            WHERE Account.Id =: acct.Id AND IsWon=FALSE]){
                        L.RP_Customer_Engagement__c = acct.Tasks.size();
                        oppsUpdatable.add(L);
                        }
                    }

            if(oppsUpdatable.size()>0){
                update oppsUpdatable;
            }
        }
    }

    public void taskCountDel(List<Task> delTasks) {

            for (Task t2 : delTasks) {
                    if (t2.WhatId!= null && t2.Status == 'Completed' && t2.RP_Prior_60__c <= t2.ActivityDate && string.valueof(t2.WhatId).startsWith(acctPrefix)){
                        OppIds.add(t2.WhatId);
                    }
            }

        if (OppIds.size() > 0){

            List<Account> acctsWithTasks = [SELECT Id,(SELECT Id FROM Tasks)
                                            FROM Account
                                            WHERE Id IN : OppIds];

                List<Opportunity> oppsUpdatable = new List<Opportunity>();

                    for(Account acct : acctsWithTasks){
                        for(Opportunity L : [SELECT Id,Account.Id,RP_Customer_Engagement__c,IsWon
                                            FROM Opportunity
                                            WHERE Account.Id =: acct.Id AND IsWon=FALSE]){
                        L.RP_Customer_Engagement__c = acct.Tasks.size();
                        oppsUpdatable.add(L);
                        }
                    }
            if(oppsUpdatable.size()>0){
                update oppsUpdatable;
            }
        }
    }
}

 
Best Answer chosen by John Neilan
John NeilanJohn Neilan
Thanks.  I figured out the issue.  I did change the condition, but I also added another set to hold the IDs of records being removed and then subtracted the count in my custom field.  Thanks for your assistance.

All Answers

Shashikant SharmaShashikant Sharma
I think the reason should be this condition: 

if (t2.WhatId!= null && t2.Status == 'Completed' && t2.RP_Prior_60__c <= t2.ActivityDate && string.valueof(t2.WhatId).startsWith(acctPrefix)){

here you have checekd for  Status == 'Completed' , so it only considers records which has Completed Status. 
John NeilanJohn Neilan
Thanks.  I figured out the issue.  I did change the condition, but I also added another set to hold the IDs of records being removed and then subtracted the count in my custom field.  Thanks for your assistance.
This was selected as the best answer