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
Vignesh RamshettyVignesh Ramshetty 

Need to update of child bases on ceteria

HI
 Below is the code which iam calling whenever  the record is inserted , updated, deleated , undeleated  i able to get the count of releated to that account But i want the filter the count based on policy status and need to update that size can anyone help on this please. 



Trigger forcounttherecords on Customer_Policy_details__c (After insert, After Update , After delete ,After undelete){


    if(trigger.isafter==true &&(trigger.isinsert== true || trigger.isupdate == true || trigger.isdelete == true || trigger.isundelete == true)){
      
                forcountingCDPrecords.Methodforcounting(trigger.new,trigger.old);
    }
 }




Public class forcountingCDPrecords{

Public Static void Methodforcounting (List<Customer_Policy_details__c> varnewCon,List<Customer_Policy_details__c> varold){


                         map<Id,Customer_Policy_details__c> varActivelist = new map<Id,Customer_Policy_details__c>();
                          map<Id,Customer_Policy_details__c> varGettingexpitedlist = new map<Id,Customer_Policy_details__c>();
                         map<Id,Customer_Policy_details__c> varexpiredlist = new map<Id,Customer_Policy_details__c>();
                         
     if (varnewCon != null){

         for(Customer_Policy_details__c varc : varnewCon){

                  if(varc.Account__c != null && varc.Policy_Status__c == 'Active'){
                    
                  varActivelist.put(varc.Account__c,varc);
                  }
                  
                  else if (varc.Account__c != null && varc.Policy_Status__c == 'Getting Expited'){
                    
                     varGettingexpitedlist.put(varc.Account__c,varc);
                  
                  }
                  else if (varc.Account__c != null && varc.Policy_Status__c == 'Expired'){
                    
                     varexpiredlist.put(varc.Account__c,varc);

           }

       }

    }

  if (varold != null){

         for(Customer_Policy_details__c varcc : varold){

  if(varcc.Account__c != null && varcc.Policy_Status__c == 'Active'){
                    
                  varActivelist.put(varcc.Account__c,varcc);
                  }
                  
      else if(varcc.Account__c != null && varcc.Policy_Status__c == 'Getting Expited'){
                  varGettingexpitedlist.put(varcc.Account__c,varcc);
       
       }   
       else if (varcc.Account__c != null && varcc.Policy_Status__c == 'Expired'){
                     varexpiredlist.put(varcc.Account__c,varcc);
       
        }         
           

       }

    }

List<Account> GettingActivelist= [SELECT id,Expired__c,Active_Policies__c,Acction_Required__c,(SELECT id,Policy_Status__c FROM Customer_Policy_details__r )
 FROM Account Where id in: varActivelist.keyset()];
 
 
 if(GettingActivelist.size() > 0) {
                   
                        for(Account a : GettingActivelist){
  
                           a.Active_Policies__c = a.Customer_Policy_details__r.size();
                           
                           
                           }
             update GettingActivelist;           
 

      }
      
 List<Account> GettingActionrequiredlist= [SELECT id,Expired__c,Active_Policies__c,Acction_Required__c,
 (SELECT id,Policy_Status__c FROM Customer_Policy_details__r )
 FROM Account Where id in: varGettingexpitedlist.keyset()];
 
 
 if(GettingActivelist.size() > 0) {
                   
                        for(Account a : GettingActionrequiredlist){
  
                           a.Acction_Required__c = a.Customer_Policy_details__r.size();
                           
                           
                           }
             update GettingActionrequiredlist;           
 

      }     
      
 List<Account> GettingExpiredlist= [SELECT id,Expired__c,Active_Policies__c,Acction_Required__c,
 (SELECT id,Policy_Status__c FROM Customer_Policy_details__r )
 FROM Account Where id in: varexpiredlist.keyset()];
 
 
 if(GettingExpiredlist.size() > 0) {
                   
                        for(Account a : GettingExpiredlist){
  
                           a.Expired__c = a.Customer_Policy_details__r.size();
                           
                           
                           }
             update GettingExpiredlist;           
 

      }      
      
      

  }

}
AnkaiahAnkaiah (Salesforce Developers) 
Hi Vignesh,

try with below code and modify the field API names as per your org.
trigger forcounttherecords on Customer_Policy_details__c (after delete,after update,after insert,after undelete) {
    
    set<ID> AccIds = new set<ID>();   
    
    if(trigger.isinsert || trigger.isundelete){
        for(Customer_Policy_details__c varc : trigger.new){
            AccIds.add(varc.Account__c);
        }
    }
    if(trigger.isdelete){
        for(Customer_Policy_details__c varc : trigger.old){
            AccIds.add(varc.Account__c);           
        }        
    }
       
    if(trigger.isupdate){
        for(Customer_Policy_details__c varc:trigger.new){
            AccIds.add(varc.Account__c);
            if(trigger.oldmap.get(varc.id).Account__c != varc.Account__c && trigger.oldmap.get(varc.id).Account__c != null ){
                AccIds.add(trigger.oldmap.get(varc.id).Account__c);
            }            
        }
    }    
	
	   list<Account> PolicyCountUpdate = new list<Account>();
	
	   AggregateResult[] groupedActiveResult = [SELECT COUNT(Id), Account__c FROM Customer_Policy_details__c where Account__c IN :AccIds AND Policy_Status__c='Active' GROUP BY Account__c ];
	   AggregateResult[] groupedGettingexpiredResult = [SELECT COUNT(Id), Account__c FROM Customer_Policy_details__c where Account__c IN :AccIds AND Policy_Status__c='Getting Expired' GROUP BY Account__c ];
	   AggregateResult[] groupedExpiredResult = [SELECT COUNT(Id), Account__c FROM Customer_Policy_details__c where Account__c IN :AccIds AND Policy_Status__c='Expired' GROUP BY Account__c ];
   
   for(AggregateResult ar:groupedActiveResult) {
     
     Id custid = (ID)ar.get('Account__c');
     
     Integer count = (INTEGER)ar.get('expr0');
     
     Account cust1 = new Account(Id=custid);
     
     cust1.Active_Policy_Count__c = count;
     
     PolicyCountUpdate.add(cust1);
      
   }
   
      for(AggregateResult ar:groupedGettingexpiredResult) {
     
     Id custid = (ID)ar.get('Account__c');
     
     Integer count = (INTEGER)ar.get('expr0');
     
     Account cust2 = new Account(Id=custid);
     
     cust2.Gettin_Expired_Policy_Count__c = count;
     
     PolicyCountUpdate.add(cust2);
      
   }
   
    for(AggregateResult ar:groupedExpiredResult) {
     
     Id custid = (ID)ar.get('Account__c');
     
     Integer count = (INTEGER)ar.get('expr0');
     
     Account cust3 = new Account(Id=custid);
     
     cust3.Gettin_Expired_Policy_Count__c = count;
     
     PolicyCountUpdate.add(cust3);
      
   }

   
   update PolicyCountUpdate;
   
}

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​
Vignesh RamshettyVignesh Ramshetty
Getting below error while insert or updateing the record

forcounttherecords: execution of AfterInsert caused by: System.ListException: Duplicate id in list: 0015g000011jpg2AAA Trigger.forcounttherecords: line 77, column 1