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
Ivan PIvan P 

Too many SOQL queries in After Update

Hello,

I have the following code that is being executed After Update:
public static void updatecasestatics(Opportunity opp, Opportunity oldOpp) {
        if (opp != oldOpp) {
            List<Case> cases = [SELECT ID, Opportunity_Name_Static__c, Opportunity_Type_Static__c, Opportunity_Unit_of_Measure_Static__c, Opportunity_Volume_Static__c, 
                                Opportunity_Comments_Static__c, Estimated_Volume_Start_Date_Static__c from Case
                                where Opportunity_Name__c = :opp.Id AND recordtype.developername='R_D_Innovation_Request'];
            if(cases != null){
                for(Case c : cases){
                    c.Opportunity_Name_Static__c = opp.Name;
                    c.Opportunity_Type_Static__c = opp.Type;
                    c.Opportunity_Unit_of_Measure_Static__c = opp.Unit_of_Measure__c;
                    c.Opportunity_Comments_Static__c = opp.Opportunity_Comments__c;
                    c.Estimated_Volume_Start_Date_Static__c = opp.Estimated_Volume_Start_Date__c;
                    c.Opportunity_Volume_Static__c = opp.Total_Primary_Opportunity_Volume__c;
                    c.Static_Fields_Changed_by_Code__c = true;
                }
                update cases;
                for(Case c : cases){
                    c.Static_Fields_Changed_by_Code__c = false;
                }
                update cases;
            }
            
        }
        
    }
When I'm updating via Data Loader 200 records in a batch I'm getting a Too Many SOQL error due to the Select that I'm doing, to try to fix it I have modified it to the following code but now I'm getting "OpportunityTrigger: System.LimitException: Apex CPU time limit exceeded"
public static void updatecasestatics(Opportunity oppo, Opportunity oldOpp) {
        
        List<Case> cases = [SELECT ID, Opportunity_Name_Static__c, Opportunity_Type_Static__c, Opportunity_Unit_of_Measure_Static__c, Opportunity_Volume_Static__c, 
                                Opportunity_Comments_Static__c, Estimated_Volume_Start_Date_Static__c, Opportunity_Name__c  from Case
                                where recordtype.developername='R_D_Innovation_Request' AND Opportunity_Name__c != null];
                                
        List<Case> casesUpdate = new List<Case>();

        for(Opportunity opp :(List<Opportunity>) Trigger.New){
            
            if(cases != null){
                for(Case c : cases){
                    if((opp.Id == c.Opportunity_Name__c) && !Approval.isLocked(c.Id)){
                        c.Opportunity_Name_Static__c = opp.Name;
                        c.Opportunity_Type_Static__c = opp.Type;
                        c.Opportunity_Unit_of_Measure_Static__c = opp.Unit_of_Measure__c;
                        c.Opportunity_Comments_Static__c = opp.Opportunity_Comments__c;
                        c.Estimated_Volume_Start_Date_Static__c = opp.Estimated_Volume_Start_Date__c;
                        c.Opportunity_Volume_Static__c = opp.Total_Primary_Opportunity_Volume__c;
                        c.Static_Fields_Changed_by_Code__c = true;
                        
                        casesUpdate.add(c);
                    }
                }
                //update cases;
                update casesUpdate;
                casesUpdate.clear();
                
                for(Case c : cases){
                    if((opp.Id == c.Opportunity_Name__c) && !Approval.isLocked(c.Id)){                    
                        c.Static_Fields_Changed_by_Code__c = false;
                        casesUpdate.add(c);
                    }
                }
               //update cases;
               update casesUpdate;
            }
            
        }
        
    }
Could you please help me? I just need to avoid the Too Many SOQL error.

Thanks, Iván.


 
ShirishaShirisha (Salesforce Developers) 
Hi Ivan,

Greetings!

Since you are processing the 200 records at a time the transcation is taking long and in result you are getting the CPU Time limit exceeded.

Can you please check the below documentation which will guide you on code which will avoid the errors in future.

https://help.salesforce.com/articleView?id=000339361&language=en_US&type=1&mode=1

https://community.zuora.com/t5/Zuora-CPQ/Error-in-Sync-System-LimitException-Apex-CPU-time-limit-exceeded/td-p/25557

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Ivan PIvan P
Hello,

I'm trying to fix the error in the first code that is Too Many SOQL Queries 101.

The second code... at the end caused another error, so I assume is not the correct way to do it.

Regards.
Ram Chand HeerekarRam Chand Heerekar
Hi Ivan,

There might be a possibility of another code running in background try checking debug logs.

 
David Zhu 🔥David Zhu 🔥
If method updatecasestatics is  a trigger handler on  Case object,  I think the trigger is in a infinite loop since AFTER Trigger is updating Case records. If that is true, you may add a static boolean in the trigger to prevent infinite loop.
If not, you may double check if there is Process on Case object. When Loading data is in batch mode, Process is still in serial mode.
Ivan PIvan P
Hello,

Let me try to add more information.

updatecasestatics is a method in the after update of Opportunity trigger handler.

The following code is working and currently used in Production, but it fails when I try to udpate more than 80 records using Data Loader (Batch Size > 80).
public static void updatecasestatics(Opportunity opp, Opportunity oldOpp) {
        if (opp != oldOpp) {
            List<Case> cases = [SELECT ID, Opportunity_Name_Static__c, Opportunity_Type_Static__c, Opportunity_Unit_of_Measure_Static__c, Opportunity_Volume_Static__c, 
                                Opportunity_Comments_Static__c, Estimated_Volume_Start_Date_Static__c from Case
                                where Opportunity_Name__c = :opp.Id AND recordtype.developername='R_D_Innovation_Request'];
            if(cases != null){
                for(Case c : cases){
                    c.Opportunity_Name_Static__c = opp.Name;
                    c.Opportunity_Type_Static__c = opp.Type;
                    c.Opportunity_Unit_of_Measure_Static__c = opp.Unit_of_Measure__c;
                    c.Opportunity_Comments_Static__c = opp.Opportunity_Comments__c;
                    c.Estimated_Volume_Start_Date_Static__c = opp.Estimated_Volume_Start_Date__c;
                    c.Opportunity_Volume_Static__c = opp.Total_Primary_Opportunity_Volume__c;
                    c.Static_Fields_Changed_by_Code__c = true;
                }
                update cases;
                for(Case c : cases){
                    c.Static_Fields_Changed_by_Code__c = false;
                }
                update cases;
            }
            
        }
        
    }
As per what I found out in the logs the problem was that the following part is being executed per every record that is being updated in the batch (using Data Loader).
List<Case> cases = [SELECT ID, Opportunity_Name_Static__c, Opportunity_Type_Static__c, Opportunity_Unit_of_Measure_Static__c, Opportunity_Volume_Static__c, 
                                Opportunity_Comments_Static__c, Estimated_Volume_Start_Date_Static__c from Case
                                where Opportunity_Name__c = :opp.Id AND recordtype.developername='R_D_Innovation_Request'];
Thanks for your help.
 
David Zhu 🔥David Zhu 🔥
Your code does not have any problem. Based on your description, it seems every record update, the trigger is triggered. You may check dataloader setting if it is in serial mode.
Ivan PIvan P
Hello,

I have checked it and it is not in serial mode.

 
David Zhu 🔥David Zhu 🔥
You may check the log what updates the opportunity records. It  is mostly likely Workflow rule and Process.