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 

Whenever the record is deleated the count is not reflecting in the account below is the code

hi,
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; 
        }
   }
}
Prateek Prasoon 25Prateek Prasoon 25

Hello! From the code you provided, it seems that you have a trigger called "For_counting_the_CPDrecords" that fires after an update or delete on the "Customer_Policy_details__c" object. The trigger calls a method called "Method_for_calucating_when_deleated" in a separate class called "updating_count_of_ploicies_in_Account" to update the count of policies in the related Account records.
It looks like the issue you are encountering is that the count of policies in the related Account records is not reflecting the deletion of records from the "Customer_Policy_details__c" object. Here are a few things you may want to check:
Make sure that the records you are deleting from the "Customer_Policy_details__c" object are associated with an Account. In the "Method_for_calucating_when_deleated" method, you are only updating the count of policies in the related Account records if the "Account__c" field on the deleted records is not null. If the deleted records are not associated with an Account, the count of policies in the related Account records will not be updated.
Check that the query in the "Method_for_calucating_when_deleated" method is returning the correct records. The query retrieves all the related Account records that have a policy record in the "varoldmap" parameter. If the query is not returning the correct records, the count of policies in the related Account records will not be updated correctly.
Make sure that the "Active_Policies__c" and "Expired__c" fields on the related Account records are updated correctly. In the "Method_for_calucating_when_deleated" method, you are decrementing the "Active_Policies__c" field by 1 if the deleted policy record has a policy status of "Active". Similarly, you are decrementing the "Expired__c" field by 1 if the deleted policy record has a policy status of "Expired". Double-check that these fields are updated correctly.
I hope this helps! Let me know if you have any other questions.

If you find this answer helpful, Please mark it as the best answer.