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
Mahesh GorrepatiMahesh Gorrepati 

I am getting to many SOQL error for this code .kindly check

trigger code:;-if(trigger.isafter && trigger.isupdate)
    {
        for(customer_policy_detail__c csold: trigger.old)
        {
            for(customer_policy_detail__c csnew : trigger.new)
            {
                if(csold.plan_status__c=='new'&& csnew.plan_status__c=='active'||csold.plan_status__c=='active'&& csnew.plan_status__c=='expired' ||
                  csold.plan_status__c=='expired'&& csnew.plan_status__c=='active')
                {
                   policystatus_to_noof_policies_on_account.updationofrecords(trigger.newmap,trigger.oldmap);
                       }
                       else if(csold.plan_status__c!=csnew.plan_status__c)
                   {
                       csnew.adderror('you can change the plan status field from'+ csold.plan_status__c+ 'to'+ csnew.plan_status__c )    ;  
                }
            
        }
    }
    }
method code:- for updation of records

/*public static void updationofrecords(map<id,customer_policy_detail__c> newrecords,map<id,customer_policy_detail__c> oldrecords )
    {
        // Get account record id from the new records. 
        set<id> accountids = new set<id>();
        if(newrecords!=null && !newrecords.isempty())
        {   
            for(customer_policy_detail__c cr :newrecords.values())
            {
                accountids.add(cr.Customer_Name__c);
            }
            // store the oldrecords inside the map collection
            map<id,customer_policy_detail__c> csoldrecords =oldrecords; 
            
            // check account id is empty or not
            if(!accountids.isempty()){
                // query to the the related account records
                list<Account> acctoupdate = [select id,name,Active_policies__c,    Expired_policies__c,Expiring_policies__c,
                                             (select id,name,customer_name__c,    plan_status__c from customer_policy_details__r where id in:newrecords.keyset())
                                             from account where id in: accountids];
                
                
                // define map to restrict for the system.listexeception : duplicateid list error
                map<id,account> updateaccount =new map<id,account>();
                //list collection to bulkify 
                //list<account> lstaccount = new list<account> ();
                if(!acctoupdate.isempty()){
                    
                    for(Account acc: acctoupdate){
                        // add null check on the related collection
                        if(acc.customer_policy_details__r!=null && !acc.customer_policy_details__r.isempty())  {
                            for(customer_policy_detail__c cs:acc.customer_policy_details__r){
                                
                                // getting the old record with the id from the current record
                                list<customer_policy_detail__c> csold =csoldrecords.get(cs.Id);
                                
                                if(csold.plan_status__c =='new'&& cs.plan_status__c=='active'){
                                    system.debug( 'the active policies count'+acc.Active_policies__c);
                                    // here it will get the previous value of the accountpolicies field and add the new records count to the number
                                    if(acc.Active_policies__c==null){
                                        acc.Active_policies__c=0;
                                    }
                                    acc.Active_policies__c+=1;
                                } 
                                // comparing the old plan status to the current status field 
                                else if (csold.plan_status__c =='active'&& cs.plan_status__c=='expired'){
                                    // here it will get the previous value of the expiredpolicies field and add the new records count to the number
                                    acc.Active_policies__c-=1;
                                   if(acc.Expired_policies__c==null){
                                        acc.Expired_policies__c=0;
                                   }
                                    acc.Expired_policies__c+=1;
                                }    /*else if(csold.plan_status__c =='expired'&& cs.plan_status__c=='active')
                                {
                                    acc.Expired_policies__c-=1;
                                    acc.Active_policies__c+=1;
                                }*/
                                
                                //add the records to the collection
                                //lstaccount.add(acc);
                                
                            /*    
                            }
                        }updateaccount.put(acc.id,acc);
                    }
                    //update acctoupdate;
                    
                }///updateaccount.putall(lstaccount );
                system.debug('The map collection ...'+updateaccount );
                if(!updateaccount.isempty())
                    //update the map collection
                    update updateaccount.values();
                
            }
            
        }
    } */
Best Answer chosen by Mahesh Gorrepati
Sai PraveenSai Praveen (Salesforce Developers) 
HI Mahesh,

You are calling another method which contains soql inside for loop because of which you are facing the error.


The code should be as below.
 
trigger code:;-if(trigger.isafter && trigger.isupdate)
   Map<id,customer_policy_detail__c> oldcusmap= new map<id,customer_policy_detail__c>();
     Map<id,customer_policy_detail__c> newcusmap= new map<id,customer_policy_detail__c>();
 
    {
        for(customer_policy_detail__c csold: trigger.old)
        {
            for(customer_policy_detail__c csnew : trigger.new)
            {
                if(csold.plan_status__c=='new'&& csnew.plan_status__c=='active'||csold.plan_status__c=='active'&& csnew.plan_status__c=='expired' ||
                  csold.plan_status__c=='expired'&& csnew.plan_status__c=='active')
                {
                    oldcusmap.putall(trigger.oldmap);
                    newcusmap.putall(trigger.newmap);
                   //policystatus_to_noof_policies_on_account.updationofrecords(trigger.newmap,trigger.oldmap);
                       }
                       else if(csold.plan_status__c!=csnew.plan_status__c)
                   {
                       csnew.adderror('you can change the plan status field from'+ csold.plan_status__c+ 'to'+ csnew.plan_status__c )    ;  
                }
            
        }
    }

    if(oldcusmap.size()>0)
    policystatus_to_noof_policies_on_account.updationofrecords(newcusmap,oldcusmap);
    }

Also another issue in the code is adding for loop inside another for loop which is very bad practice.

Let me know if you face any issues.

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

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mahesh,

can you confirm if the commented method present  and also confirm when you are getting SOQL issue. is it for bulk update or for single record as well?

Thanks,
 
Mahesh GorrepatiMahesh Gorrepati
i am getting the error for bullrecords,
for single it is processing the record and updating
the commented code is the class method
Sai PraveenSai Praveen (Salesforce Developers) 
HI Mahesh,

You are calling another method which contains soql inside for loop because of which you are facing the error.


The code should be as below.
 
trigger code:;-if(trigger.isafter && trigger.isupdate)
   Map<id,customer_policy_detail__c> oldcusmap= new map<id,customer_policy_detail__c>();
     Map<id,customer_policy_detail__c> newcusmap= new map<id,customer_policy_detail__c>();
 
    {
        for(customer_policy_detail__c csold: trigger.old)
        {
            for(customer_policy_detail__c csnew : trigger.new)
            {
                if(csold.plan_status__c=='new'&& csnew.plan_status__c=='active'||csold.plan_status__c=='active'&& csnew.plan_status__c=='expired' ||
                  csold.plan_status__c=='expired'&& csnew.plan_status__c=='active')
                {
                    oldcusmap.putall(trigger.oldmap);
                    newcusmap.putall(trigger.newmap);
                   //policystatus_to_noof_policies_on_account.updationofrecords(trigger.newmap,trigger.oldmap);
                       }
                       else if(csold.plan_status__c!=csnew.plan_status__c)
                   {
                       csnew.adderror('you can change the plan status field from'+ csold.plan_status__c+ 'to'+ csnew.plan_status__c )    ;  
                }
            
        }
    }

    if(oldcusmap.size()>0)
    policystatus_to_noof_policies_on_account.updationofrecords(newcusmap,oldcusmap);
    }

Also another issue in the code is adding for loop inside another for loop which is very bad practice.

Let me know if you face any issues.

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

Thanks,
This was selected as the best answer
Mahesh GorrepatiMahesh Gorrepati
Thank you. 
 I modified the code with the single loop also
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Mahesh,

Is the issue resolved now?

Thanks,
 
Mahesh GorrepatiMahesh Gorrepati
yes . thankyou
 can you help me the other question regarding the allrows key word in relation ?

can we use ALLROWS for the relation query. kindly check the below code;
list<Account> acctoupdate = [select id,name,Active_policies__c,    Expired_policies__c,Expiring_policies__c,
                                             (select id,name,customer_name__c,    plan_status__c from customer_policy_details__r where id in:newrecords.keyset() All rows)
                                             from account where id in: accountids];
Sai PraveenSai Praveen (Salesforce Developers) 
Hi mahesh,

you cannot add ALLROWS in sub query it gives you error.

Thanks
 
Mahesh GorrepatiMahesh Gorrepati
Thank you!