You need to sign in to do that
Don't have an account?
Ivan P
Too many SOQL queries in After Update
Hello,
I have the following code that is being executed After Update:
Thanks, Iván.
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.
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
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.
There might be a possibility of another code running in background try checking debug logs.
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.
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). 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). Thanks for your help.
I have checked it and it is not in serial mode.