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 

The error with the screen short

HI
 i have Customer_Policy_details__c object which is child to account which contines Expired, Active, getting expired records so, when ever the policy status changes to Getting expired or expired i want to update the count in account object below is the code please help where i went wrong

ERROR : I have taken screenshotUser-added image



Public class forcount{

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


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

         for(Customer_Policy_details__c varc : varnewCon){

                  if(varc.Account__c != null ){
                    
                  varmaplist.put(varc.Account__c,varc);

           }

       }

    }

  if (varold != null){

         for(Customer_Policy_details__c varcc : varold){

                       if(varcc.Account__c != null )
{
                    
                  varmaplist.put(varcc.Account__c,varcc);

           }

       }

    }

List<Account> varacclist = [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: varmaplist.keyset() AND Account.Customer_Policy_details__r.Policy_Status__c = 'Active'];
    
    
List<Account> varacclist1 = [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: varmaplist.keyset() AND Account.Customer_Policy_details__r.Policy_Status__c = 'Getting Expited'];
    
    List<Account> varacclist2 = [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: varmaplist.keyset() AND Account.Customer_Policy_details__r.Policy_Status__c = 'Expired'];
    
    if(varacclist.size() > 0) {
                   
                        for(Account a : varacclist2  ){
                            for(Customer_Policy_details__c csd:a.Customer_Policy_details__r)
{
system.debug('into this');
 system.debug('size'+a.Customer_Policy_details__r.size());
                             if(csd.Policy_Status__c == 'Expired'){

                           a.Expired__c = a.Customer_Policy_details__r.size();
          }
    
                }
                            
                        }
                                                   for(Account a : varacclist1  ){
                            for(Customer_Policy_details__c csd:a.Customer_Policy_details__r)
{

               if(csd.Policy_Status__c == 'Getting Expited'){

                           a.Acction_Required__c = a.Customer_Policy_details__r.size();

           }
    
                            }
                   }
        
                                                           for(Account a : varacclist  ){
                            for(Customer_Policy_details__c csd:a.Customer_Policy_details__r)
{
        
              if(csd.Policy_Status__c == 'Active'){

                           a.Active_Policies__c = a.Customer_Policy_details__r.size();

           }
}
     }                                                           
        }
}

       update varacclist; 
    
 update varacclist1;
    update varacclist2;
      }

  }

}
 
AnkaiahAnkaiah (Salesforce Developers) 
Hi Vignesh,

try with below code.
trigger UpdateHeadCount on Customer_Policy_details__c (after insert, after update,after Delete) {

   List<Account> Policycountupdate = new List<Account>();
   
   Set<Id> DptIds = new Set<Id>();
   
   if(Trigger.isUpdate) {

     for(Customer_Policy_details__c cpd:Trigger.New) {
      
        DptIds.add(cpd.Account__c);   
    
     }

     for(Customer_Policy_details__c cpd:Trigger.Old) {
      
        DptIds.add(cpd.Account__c);   
    
     }   
   
   }
if(Trigger.IsInsert) {
     for(Customer_Policy_details__c cpd:Trigger.New) {
      
        DptIds.add(cpd.Account__c);   
    
     }
   }
   
   if(Trigger.IsDelete) {
     for(Customer_Policy_details__c cpd:Trigger.old) {
      
        DptIds.add(cpd.Account__c);   
    
     }
   }
   AggregateResult[] groupedResults = [SELECT COUNT(Id), Account__c FROM Customer_Policy_details__c where Account__c IN :DptIds AND (Policy_Status__c='Getting Expired' OR Policy_Status__c='Expired') GROUP BY Account__c ];
   
   for(AggregateResult ar:groupedResults) {
     
     Id custid = (ID)ar.get('Account__c');
     
     Integer count = (INTEGER)ar.get('expr0');
     
     Account cust1 = new Account(Id=custid);
     
     cust1.ExpiredPolicycount__c = count;
     
     Policycountupdate.add(cust1);
      
   }
   update Policycountupdate;
   }

If this helps, Please mark it as best answer.

Thanks!!​​​​​​​