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
Gourav DhoteGourav Dhote 

i have written a trigger on task for update account fields it is working on insert and update and delete but on delete its not updating fields

trigger TaskTrigger on Task (after insert,before delete,after update) {
    public List<Task> ltask1 = new List<Task>();
    public id acid;
    public integer clo=0;   
    public integer ope=0;
    public integer blk=0;
    if(trigger.isinsert || trigger.isupdate){  
    for(Task t:Trigger.New){
        acid = t.WhatId;
        system.debug('accid'+acid);
        
    }
    ltask1 = [select id,Status from task where whatid=:acid];
    system.debug('acsize'+ltask1.size());
    
    
    for(task t:ltask1){
        if(t.Status=='Completed'){
            clo = clo+1;
            system.debug(clo);
        } else if(t.Status=='In Progress'){
            ope = ope +1;
            system.debug(ope);
        }else if(t.Status=='Deferred')
        {
             blk = blk+1;
            system.debug(blk);
        }                
    } List<Account> acc = new List<Account>();    
    List<Account> ac = [select id from Account where id = :acid];
    system.debug('oppsize'+ac.size());
    for(Account a: ac){
        a.Open_Task__c = ope;
        a.Closed_Tasks__c = clo;
        a.Blocked_Tasks__c=blk;
        acc.add(a);  
    } if(acc.size()>0){
        update acc;
    }   
    }
        //ltask = [select id,Status from task where whatid=:acid];
        
   
   
    if (Trigger.isDelete){
              for (Task t : trigger.Old) {
                   acid=t.whatid;
               }
    ltask1 = [select id,Status from task where whatid=:acid];
    system.debug('acsize'+ltask1.size());
    
    
    for(task t:ltask1){
        if(t.Status=='Completed'){
            clo = clo-1;
            system.debug(clo);
        } else if(t.Status=='In Progress'){
            ope = ope -1;
            system.debug(ope);
        }else if(t.Status=='Deferred')
        {
             blk = blk-1;
            system.debug(blk);
        }                
    } List<Account> acc = new List<Account>();    
    List<Account> ac = [select id from Account where id = :acid];
    system.debug('oppsize'+ac.size());
    for(Account a: ac){
        a.Open_Task__c = ope;
        a.Closed_Tasks__c = clo;
        a.Blocked_Tasks__c=blk;
        acc.add(a);  
    } if(acc.size()>0){
        update acc;
    }   
    }
      
    system.debug('No of Tasks'+ope+ clo+blk);

    }
        
Lukesh KarmoreLukesh Karmore
Hi, Gourav Dhote 
Please use after delete, and try

  if (trigger.isAfter && trigger.isDelete){
              for (Task t : trigger.Old) {
                   acid=t.whatid;
               }
    ltask1 = [select id,Status from task where whatid=:acid];
    system.debug('acsize'+ltask1.size());
    
    list<task> tasklist=new list<task>();
    for(task t:ltask1){
        if(t.Status=='Completed'){
            clo = clo-1;
           tasklist.add(t);
            system.debug(clo);
        } else if(t.Status=='In Progress'){
            ope = ope -1;
            tasklist.add(t);
            system.debug(ope);
        }else if(t.Status=='Deferred')
        {
             blk = blk-1;
            tasklist.add(t);
            system.debug(blk);
        }                
    }
  update   tasklist;
List<Account> acc = new List<Account>();    
    List<Account> ac = [select id from Account where id = :acid];
    system.debug('oppsize'+ac.size());
    for(Account a: ac){
        a.Open_Task__c = ope;
        a.Closed_Tasks__c = clo;
        a.Blocked_Tasks__c=blk;
        acc.add(a);  
    } if(acc.size()>0){
        update acc;
    }   
    }
      
    system.debug('No of Tasks'+ope+ clo+blk);

    }
If the above solution helps you, please mark the best answer, It will helps others.
Thanks,
Lukesh 
PriyaPriya (Salesforce Developers) 
Hi

Add one more event to your trigger "After delete".

Kindly mark it as the best asnwer if it works.

Thanks,
Priya Ranjan