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
SV MSV M 

Count Tasks related to Opportunity

Hi, I have an apex trigger which displays total tasks related to opportunity, but my trigger is not working even it doesn't have any errors. Can someone help me with this trigger?

//Apex Trigger

trigger OpportunityTaskCount on Task (after insert, after update, after delete, after undelete) {
    Set<Id> oppList = new Set<Id>();
    Set<Id> tskList = new Set<Id>();
    List<Opportunity> updateList = new List<Opportunity>();
    if(trigger.isInsert) {
        for(Task tsk : trigger.new) {
            if(tsk.WhatId != NULL) {
                oppList.add(tsk.Id);
                tskList.add(tsk.WhatId);
            }
        }
        system.debug('Opportunities'+oppList);
    }
    if(trigger.isDelete || trigger.isUpdate ) {
        for(Task tsk : trigger.old) {
            if(tsk.WhatId != NULL) {
                oppList.add(tsk.Id);
                tskList.add(tsk.WhatId);
            }
        }
    }
    if(trigger.isUndelete) {
        for(Task tsk : trigger.new) {
            if(tsk.WhatId != NULL) {
                tskList.add(tsk.WhatId);
            }
        }
    }
    for (Opportunity opp : [SELECT Id, Name, Total_Tasks__c ,(SELECT Id, Status FROM Tasks) 
                           FROM Opportunity 
                           WHERE Id in : oppList]) {
                               opp.Total_Tasks__c = opp.Tasks.size();
                               updateList.add(opp);
                           }
    update updateList;
}
Best Answer chosen by SV M
MagulanDuraipandianMagulanDuraipandian
oppList contains Task Ids not Opportunities Ids. You are querying Opportunities using Task Ids.

--
Magulan Duraipandian
https://www.infallibletechie.com/

All Answers

MagulanDuraipandianMagulanDuraipandian
oppList contains Task Ids not Opportunities Ids. You are querying Opportunities using Task Ids.

--
Magulan Duraipandian
https://www.infallibletechie.com/
This was selected as the best answer
Foram Rana RForam Rana R
Hi Vineeth,

Please use below code :
 
//Apex Trigger

trigger OpportunityTaskCount on Task (after insert, after update, after delete, after undelete) {
    Set<Id> oppList = new Set<Id>();
    Set<Id> tskList = new Set<Id>();
    List<Opportunity> updateList = new List<Opportunity>();
    if(trigger.isInsert) {
        System.debug('@@@ In Trigger');
        for(Task tsk : trigger.new) {
            if(tsk.WhatId != NULL) {
                oppList.add(tsk.Id);
                tskList.add(tsk.WhatId);
            }
        }
        system.debug('Opportunities'+oppList);
    }
    if(trigger.isDelete || trigger.isUpdate ) {
        for(Task tsk : trigger.old) {
            if(tsk.WhatId != NULL) {
                oppList.add(tsk.Id);
                tskList.add(tsk.WhatId);
            }
        }
    }
    if(trigger.isUndelete) {
        for(Task tsk : trigger.new) {
            if(tsk.WhatId != NULL) {
                tskList.add(tsk.WhatId);
            }
        }
    }
    for (Opportunity opp : [SELECT Id, Name, Total_Tasks__c ,(SELECT Id, Status FROM Tasks) 
                            FROM Opportunity 
                            WHERE Id in : tskList]) {
                                System.debug('@@@ opp.Tasks  = '+opp.Tasks);
                                opp.Total_Tasks__c = opp.Tasks.size();
                                updateList.add(opp);
                            }
    update updateList;
}

Hope this helps you.
If this helps kindly mark it as solved so that it may help others in the future.

Thanks & Regards,
Foram Rana