• Ivan P
  • NEWBIE
  • 0 Points
  • Member since 2019

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 3
    Replies
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.


 
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.