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
JGrafJGraf 

"Too Many SOQL Queries" on Trigger for User but not SysAdmin

I am an experienced Administrator but a very, very green Developer.  Someone was nice enough to write this trigger for me and it worked great in my sandbox and it still works great for me in Production but my users are getting the infamous "Too Many SOQL Queries" message while I do same exact thing and it runs fine.  Is there something I am missing?

This is the code.
 trigger UpdateEQLineOnProjTask on AcctSeed__Project_Task__c (after update) {
    
    try{
        List<AcctSeed__Project_Task__c>  projectTaskList = new List<AcctSeed__Project_Task__c>();
        Map<String,Specification_Default__c> specDefaultMap  = new Map<String,Specification_Default__c>();
        for(Specification_Default__c specDefault : [Select Id,Name from Specification_Default__c ]){
            specDefaultMap.put(specDefault.Id,specDefault);
        }
        List<EQLines__c> addEqLineList = new List<EQLines__c>();
        List<EQLines__c> deleteEqLineList = new List<EQLines__c>();
        for(AcctSeed__Project_Task__c  prjcttask:Trigger.new){
            if(prjcttask.Model_Number_1__c!=null)
            {
                
                List<EQLines__c>  eqLineList = [Select Id, Name from EQLines__c Where Project_Task__c =:prjcttask.Id ];
                if(eqLineList.size()>0){
                    if(prjcttask.Model_Number_1__c != Trigger.oldMap.get(prjcttask.Id).Model_Number_1__c){
                        deleteEqLineList = eqLineList;
                        //This Query needs to be modified in case you add new fields to EQLine
                        List<EQLines__c>  newEqLineList = [Select Id, Name, AI__c, Amps__c, AO__c, DI__c, Dimensions__c,DO__c,HP__c, Manufacturer__c,Manufacturer_Model__c, Material_of_Construction__c, Max_Flow__c , Max_Pressure__c,Shipping_Notes__c,Transmitter_Type__c,Voltage__c,WWW_Part_Number__c, Process_Fluid__c from EQLines__c Where Specification_Default__c =:prjcttask.Model_Number_1__c ];
                        Specification_Default__c SD = new Specification_Default__c();
                        if(specDefaultMap.size()>0)
                            SD = specDefaultMap.get(prjcttask.Model_Number_1__c);
                        List<EQLines__c> clonedEqLineList = newEqLineList.deepClone(false,false,false);
                        addEqLineList.clear();
                        for(EQLines__c eqLine: clonedEqLineList){
                            eqLine.Name = eqLine.Name+SD.Name+prjcttask.Name;
                            eqLine.Project_Task__c = prjcttask.Id;
                            addEqLineList.add(eqLine);
                        }
                    }
                }
                else{
                    //This Query needs to be modified in case you add new fields to EQLine
                    List<EQLines__c>  newEqLineList = [Select Id, Name, AI__c, Amps__c, AO__c, DI__c, Dimensions__c,DO__c,HP__c, Manufacturer__c,Manufacturer_Model__c, Material_of_Construction__c, Max_Flow__c , Max_Pressure__c,Shipping_Notes__c,Transmitter_Type__c,Voltage__c,WWW_Part_Number__c, Process_Fluid__c from EQLines__c Where Specification_Default__c =:prjcttask.Model_Number_1__c ];
                    Specification_Default__c SD = new Specification_Default__c();
                    if(specDefaultMap.size()>0)
                        SD = specDefaultMap.get(prjcttask.Model_Number_1__c);
                    List<EQLines__c> clonedEqLineList = newEqLineList.deepClone(false,false,false);
                    addEqLineList.clear();
                    for(EQLines__c eqLine: clonedEqLineList){
                        eqLine.Name = eqLine.Name+SD.Name+prjcttask.Name;
                        eqLine.Project_Task__c = prjcttask.Id;
                        addEqLineList.add(eqLine);
                    }
                }           
            }
            else{
                deleteEqLineList = [Select Id, Name from EQLines__c Where Project_Task__c =:prjcttask.Id ];
            }
        }
        delete deleteEqLineList;
        insert addEqLineList;
    }
    catch(Exception e){
        system.debug('The following error occured ' + e.getMessage());
    }
}

If it helps, the error is occuring when they hit a button that is a Deep Clone of the grandparent object of the EQLine Object in this trigger.  When the new trigger is run on an individual level, this seems to not be an issue.  In the very least, I need to know how to deactivate the trigger temporarily.  Hopefully this unexpected adventure will kick start my understanding of the dev side of Salesforce.
Thanks!
SivaGSivaG
Hi,

The issue here is 3 SOQL queries are present inside the FOR loop.This trigger needs to be restructed to avoid this issue.

Thanks
Kumar
Jake BackuesJake Backues
As Kumar G said you need to restructure this trigger. You need to get your soql query out of the way before you go into the logic in the for loop. You need to add something like this before you go into the loop:

Set<String> ModelNumberOne_Set = new Set<String>();
        for(AcctSeed__Project_Task__c  prjcttask:Trigger.new){
            if(prjcttask.Model_Number_1__c !=null){
                ModelNumberOne_Set.add(string.valueOf(prjcttask.Model_Number_1__c));
            }
        }
        List<EQLines__c>  newEqLineList;
        if(ModelNumberOne_Set.size() > 0){
            newEqLineList = [Select Id, Name, AI__c, Amps__c, AO__c, DI__c, Dimensions__c,DO__c,HP__c, Manufacturer__c,
                        Manufacturer_Model__c, Material_of_Construction__c, Max_Flow__c , Max_Pressure__c,Shipping_Notes__c,
                        Transmitter_Type__c,Voltage__c,WWW_Part_Number__c, Process_Fluid__c 
                           from EQLines__c 
                                  Where Specification_Default__c IN :ModelNumberOne_Set ];
        }