• Gourav Dhote
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 0
    Replies
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);

    }
        
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);

    }