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 

unable to update account when Cdp is deleated Can any one help please

hi,
unable to update account, Whenever the record is deleated the count is not reflecting in the account below is the code

Trigger For_counting_the_CPDrecords on Customer_Policy_details__c (after update,after delete){

 if(trigger.isafter == true && trigger.isdelete == true){

                                 updating_count_of_ploicies_in_Account.Method_for_calucating_when_deleated(trigger.oldMap);

 }
 
 

public class  updating_count_of_ploicies_in_Account {
Public static void Method_for_calucating_when_deleated (map<id,Customer_Policy_details__c> varoldmap){

             map<id,Customer_Policy_details__c>  varmaplist = new map<id,Customer_Policy_details__c>();

                system.debug(varoldmap);
          for(Customer_Policy_details__c varc : varoldmap.values()){

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

       List<Account> Addingrecords = [SELECT id,Expired__c,Active_Policies__c,(SELECT id,Policy_Status__c FROM Customer_Policy_details__r where id in: varoldmap.keyset())
                                      FROM Account WHERE id IN:varmaplist.keyset()];
         system.debug(Addingrecords.size());
    
    if(Addingrecords.size() > 0) {
 for(Account vara : Addingrecords){
  system.debug(Addingrecords);
     
 for(Customer_Policy_details__c a : vara.Customer_Policy_details__r){
     
 system.debug(vara.Customer_Policy_details__r);
               if(a.Policy_Status__c == 'Active'){
                    system.debug(vara.Active_Policies__c);
                  vara.Active_Policies__c -=1;
                 system.debug(vara.Active_Policies__c);
              }
                if(a.Policy_Status__c == 'Expired'){
                 
                        vara.Expired__c -=1;
                 
               }
         }
 }
         update Addingrecords; 
        }
   }
}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vignesh,

Did you debug the code and checked at what point exactly the code is failing?

Thanks,
 
Vignesh RamshettyVignesh Ramshetty
Yes Sir,
           In the last for loop Below is the line for your reference from their is record is missing

 for(Customer_Policy_details__c a : vara.Customer_Policy_details__r){
     
 system.debug(vara.Customer_Policy_details__r);
Thank you,
Vignesh