Jul 4, 2024
19 Views
Comments Off on Salesforce Apex Trigger – Count Number of Open Tasks and Closed Tasks on the Account.
0 0

Salesforce Apex Trigger – Count Number of Open Tasks and Closed Tasks on the Account.

Written by

Hello guys,

Today, I’m sharing the trigger code that how you can count the number of open tasks and closed task on the account. For this, you need to make to custom fields on Account. They are –

  1. Open Task field.
  2. Closed Task field.

Apex Trigger code –

trigger countOpenAndClosedTask on Task (after insert, after undelete, after delete, after update) {    
    Set<Id> setOpenTaskAccountIds = new Set<Id>();
    Set<Id> setClosedTaskAccountIds = new Set<Id>();
    List<Account> listAccountUpdate = new List<Account>();
    List<Account> listOpenTaskAccounts = new List<Account>();
    List<Account> listClosedTaskAccounts = new List<Account>();
    if(Trigger.IsAfter){
        if(Trigger.IsInsert || Trigger.IsUndelete  || Trigger.isUpdate) {
            for(Task t: Trigger.new) {
                if(String.valueOf(t.WhatId).startsWithIgnoreCase('001')) {
                    if(t.Status.equalsIgnoreCase('Completed')){
                        setClosedTaskAccountIds.add(t.WhatId);
                        System.debug('@@@@Inside Completed@@@'+setClosedTaskAccountIds);
                    }
                    else {
                        setOpenTaskAccountIds.add(t.WhatId);
                        System.debug('@@@@Inside Open@@@'+setOpenTaskAccountIds);
                    }
                    if(Trigger.IsUpdate){
                        if(Trigger.oldMap.get(t.Id).WhatId != t.WhatId){
                            if(t.Status.equalsIgnoreCase('Completed')){
                                setClosedTaskAccountIds.add(Trigger.oldMap.get(t.Id).WhatId );
                                System.debug('@@@Inside Update Closed@@@'+setClosedTaskAccountIds);
                            }
                            else {
                                setOpenTaskAccountIds.add(Trigger.oldMap.get(t.Id).WhatId );
                                System.debug('@@@Inside Update Open@@@'+setOpenTaskAccountIds);
                            }
                        }
                    }
                }
            }
        }        
        if(Trigger.IsDelete){
            for(Task t : Trigger.Old){
                if(String.valueOf(t.WhatId).startsWithIgnoreCase('001')) {
                    if(t.Status.equalsIgnoreCase('Completed')){
                        setClosedTaskAccountIds.add(t.WhatId);
                        System.debug('@@@@Inside Deleted Completed');
                    }
                    else {
                        setOpenTaskAccountIds.add(t.WhatId);
                        System.debug('@@@@Inside Deleted Open');
                    }
                }
            }
        }            
        listOpenTaskAccounts =[SELECT Id, Name, Open_Task__c,(Select Status From Tasks Where Status != 'Completed' ) FROM Account WHERE Id =:setOpenTaskAccountIds];
        listClosedTaskAccounts =[SELECT Id, Name, Closed_Task__c,(Select Status From Tasks Where Status = 'Completed' )  FROM Account WHERE Id =:setClosedTaskAccountIds];
        System.debug('@@@@ListOpenTaskAccounts'+listOpenTaskAccounts );
        System.debug('@@@@ListClosedTaskAccounts'+listClosedTaskAccounts );       
        for(Account acc:listOpenTaskAccounts) {
            List<Task> listTask = acc.Tasks;
            acc.Open_Task__c = listTask.size();
            listAccountUpdate.add(acc);           
        }
        for(Account acc:listClosedTaskAccounts) {
            List<Task> listTask = acc.Tasks;
            acc.Closed_Task__c = listTask.size();
            listAccountUpdate.add(acc);           
        }
        try{
            System.debug('@@@@listAccountUpdate'+listAccountUpdate);
            update listAccountUpdate;
        }catch(System.Exception e){
            System.debug('@@@error'+e.getMessage());
        }
    }
}

Thanks.

Happy coding.

Article Categories:
Others
Priya Singh
https://thestarbiznews.com/

Expertise: Technology Trends, Startups, Digital Disruption Bio: Priya Singh is a tech-savvy millennial with a finger on the pulse of the ever-evolving digital landscape. A graduate of Stanford's Computer Science program and a former engineer at a Silicon Valley giant, Priya has a deep understanding of the technologies shaping our future. Her passion lies in demystifying complex tech jargon and exploring the disruptive potential of emerging trends, making her articles essential reading for anyone who wants to stay ahead of the curve. When not scouring the web for the latest tech tidbits, Priya enjoys traveling to off-the-beaten-path destinations and immersing herself in diverse cultures, always seeking new inspiration and insights.