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 

Error: Compile Error: Variable does not exist: Policy_Status__c at line 43 column 35

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


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.Policy_Status__c == 'Getting Expited' || varc.Policy_Status__c == 'Expired' ){
                    
                  varmaplist.put(varc.Account__r.id,varc);

           }

       }

    }

  if (varold != null){

         for(Customer_Policy_details__c varcc : varold){

                       if(varcc.Policy_Status__c == 'Getting Expited' || varcc.Policy_Status__c == 'Expired' || varcc.Policy_Status__c == 'Active')
{
                    
                  varmaplist.put(varcc.Account__r.id,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  ){

                             if(a.Policy_Status__c == 'Expired'){

                           a.Expired__c = a.Customer_Policy_details__r.size();
          }

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

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

           }
        }

       update varacclist; 

      }

  }

}
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you confirm if you have Policy_Status__c field on Account object?

Thanks,
 
Vignesh RamshettyVignesh Ramshetty
No i have that field on Customer_Policy_details__r
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vignesh,

You need to iterate through child records to check the condition on child object like below,
 
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.Policy_Status__c == 'Getting Expited' || varc.Policy_Status__c == 'Expired' ){
                    
                  varmaplist.put(varc.Account__r.id,varc);

           }

       }

    }

  if (varold != null){

         for(Customer_Policy_details__c varcc : varold){

                       if(varcc.Policy_Status__c == 'Getting Expited' || varcc.Policy_Status__c == 'Expired' || varcc.Policy_Status__c == 'Active')
{
                    
                  varmaplist.put(varcc.Account__r.id,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)
{

                             if(csd.Policy_Status__c == 'Expired'){

                           a.Expired__c = a.Customer_Policy_details__r.size();
          }

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

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

           }
        }
}

       update varacclist; 

      }

  }

}

Check the below link (https://www.sfdcpoint.com/salesforce/relationship-query-apex-salesforce/) for more details on it.

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Vignesh RamshettyVignesh Ramshetty
Able to save the class but the count is not updating in the account record  with the above code





 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Vignesh,

Did you get chance to check if the code is going into If statement by using debug?

Thanks,
 
Vignesh RamshettyVignesh Ramshetty
Noo unable to get 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

There were some minor issues in the code. I have updated it. can you check now.
 
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()];

    
    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; 

      }

  }

}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
Vignesh RamshettyVignesh Ramshetty
Its giving complete count i need from that list how many expired & getting expired & Expired count in their respective fields of account , But is updating complete count in the 3 fields