function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
John NeilanJohn Neilan 

Illegal assignment from SObject to SOBJECT:Task

Hello,

I created a trigger that calls a class to count the number of tasks related to an Opportunity, who's Status is "Completed".  The trigger works properly when tasks are inserted, moved to Completed, deleted, or un-deleted.  However, nothing happens if a task is moved from Completed to another status and I would like the count to be decreased when this occurs.  Can anyone help me figure this out?  Thanks,

Trigger:
trigger MainTriggerTask on Task (after insert, after update, after delete, after undelete) {
    
        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.taskCountUpdate(Trigger.new, Trigger.old);
        }
}

Class:
public class ClassRenewalTaskCount {

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

        String oppPrefix = Opportunity.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(oppPrefix) ) {
                    OppIds.add(t.WhatId);              
                }
        }

        if (OppIds.size() > 0){

            List<Opportunity> oppsWithTasks = [SELECT Id,RP_Customer_Engagement__c,(SELECT Id FROM Tasks)
                                                FROM Opportunity
                                                WHERE Id IN : OppIds];

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

                    for(Opportunity L : oppsWithTasks){
                        L.RP_Customer_Engagement__c = L.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(oppPrefix) ) {
                    OppIds.add(t.WhatId);              
                }
                if (t.WhatId != null && ((prev.Status == 'Completed' && t.Status <> 'Completed') || t.RP_Prior_60__c > t.ActivityDate) && string.valueof(t.WhatId).startsWith(oppPrefix) ) {
                    OppIds.remove(t.WhatId);
                }
            }
        }

        if (OppIds.size() > 0){

            List<Opportunity> oppsWithTasks = [SELECT Id,RP_Customer_Engagement__c,(SELECT Id FROM Tasks)
                                                FROM Opportunity
                                                WHERE Id IN : OppIds];

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

                    for(Opportunity L : oppsWithTasks){
                        L.RP_Customer_Engagement__c = L.Tasks.size();
                    oppsUpdatable.add(L);
                    }

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

    public void taskCountDel(List<Task> delTasks) {

            for (Task t2 : delTasks) {
                for (Task prev2 : (List<Task>)Trigger.Old){
                    if (t2.WhatId!= null && (t2.Status == 'Completed' || (prev2.Status == 'Completed' && t2.Status != 'Completed') || t2.RP_Prior_60__c > t2.ActivityDate) && string.valueof(t2.WhatId).startsWith(oppPrefix)){
                        OppIds.add(t2.WhatId);
                    }
                }
            }

        if (OppIds.size() > 0){

            List<Opportunity> oppsWithTasks = [SELECT Id,RP_Customer_Engagement__c,(SELECT Id FROM Tasks)
                                                FROM Opportunity
                                                WHERE Id IN : OppIds];

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

                    for(Opportunity L : oppsWithTasks){
                        L.RP_Customer_Engagement__c = L.Tasks.size();
                    oppsUpdatable.add(L);
                    }

            if(oppsUpdatable.size()>0){
                update oppsUpdatable;
                //update all the leads with activity count
            }
        }
    }
}

 
Best Answer chosen by John Neilan
John NeilanJohn Neilan
Thanks for the assistance.  I ended up adding another set to hold the IDs of records being removed and then subtracted the count in my custom field.  Now it works as expected.

All Answers

SonamSonam (Salesforce Developers) 
Could you please specify the line number where this error is coming or highlight this in the code you have shared.
John NeilanJohn Neilan
Hi Sonam.  Sorry, my subject is incorrect.  I was getting that error but I fixed it and just forgot to change the subject of my post.  My issue is that the trigger does not decrease the Activity count when an activity is moved from the Completed Status.
SonamSonam (Salesforce Developers) 
Just wondering if this is because of the below line in the class:

if (t.WhatId != null && ((prev.Status == 'Completed' && t.Status <> 'Completed') || t.RP_Prior_60__c > t.ActivityDate) && string.valueof(t.WhatId).startsWith(oppPrefix) )

The status check is in OR - I think it should be compared as an AND, could you please test this?

if (t.WhatId != null && ((prev.Status == 'Completed' && t.Status <> 'Completed') && t.RP_Prior_60__c > t.ActivityDate) && string.valueof(t.WhatId).startsWith(oppPrefix) )
John NeilanJohn Neilan
Thanks for the assistance.  I ended up adding another set to hold the IDs of records being removed and then subtracted the count in my custom field.  Now it works as expected.
This was selected as the best answer