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 

I need count of records base on Policy status

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. 

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> 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()];
 
 
 if(varacclist.size() > 0) {
                   
                        for(Account a : varacclist  ){
                            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();
          }

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

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

           }
              else   if(csd.Policy_Status__c == 'Active'){

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

           }
        }
}

       update varacclist; 

      }

  }

}