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
kathyanikathyani 

Unit Tests: System.Exception: Too many query rows

I am creating a unit test and I have insert employeeList which has a list of employees.

This is the error I am seeing:
.EmployeeTrigger: line 12, column 33: SOQL query with 49 rows finished in 4 ms
System.Exception: Too many query rows: 532

Trigger.EmployeeTrigger: line 12, column 33



 It is failing at this line [select d.Id, d.Name__c from Department__c d] in employeetrigger.

I am not clear with the line System.Exception: Too many query rows: 532. Where is it getting this number 532 from when there are only 49 rows being queried from the table.

Please let me know.


kathyanikathyani
If I say limit 100, it is still failing.
HarmpieHarmpie
Can you post some more code?
hisrinuhisrinu

Hi Kathyani,

  You are invoking that apex class from trigger, so the origin point is trigger.
  Then the query limit will be 20 not the 100.

kathyanikathyani
Here is the whole code.

trigger EmployeeTrigger on Employee__c (before insert, after insert) {
    String fName;
    String lName;
    String[] names;
    Map<String, Employee__c> supMap = new Map<String, Employee__c>();
    List<Employee__c> supUpdates = new List<Employee__c>();
    Employee__c sup;
    Employee__c emp;
    Employee__c ceo;
    
    
    Integer myDMLLimit = Limits.getLimitQueryRows();
    System.debug('myDMLLimit &&&&&&&&&&&&&&&&&' + myDMLLimit);
    
    
    Map<String, String>deptIds = new Map<String, String>();
    List<Department__c> depts = [select d.Id, d.Name__c from Department__c d];
    
        
    for (Department__c d : depts) {
        deptIds.put(d.Name__c, d.Id);
    }
    
    for (Employee__c e : Trigger.new) {
        if (e.Name != null) { // Don't want to fire this from an insert from Flex
            if (Trigger.isBefore) {
                    names = e.Name.split(',');
                    e.LName__c = names[0];
                    e.FName__c = names[1];
                    e.Department__c = deptIds.get(e.DeptName__c);
                    e.EmploymentStatus__c = 'A';
                    System.debug(e.Department__c);
            }
            else {
                supMap.put(e.Name, e);
            }
        }
    }
    
    if (Trigger.isAfter) {
        Department__c company = [select d.Id, Name__c from Department__c d where d.Parent__c = null];
        System.debug([select count() from Employee__c where  Department__c = :company.Id]);
        for (Employee__c e1 : [select e.Id, e.LName__c from Employee__c e where e.Department__c = :company.Id]) {
            System.debug(e1);    
        }
        ceo = [select e.Id from Employee__c e where e.Department__c = :company.Id];
        Map<ID, Employee__c> emps = new Map<ID, Employee__c>([select e.Id, e.Supervisor__c, e.Name from Employee__c e]);
        for (Employee__c ee : Trigger.new) {
            if (ee.Supervisor__c == null) { //Don't execute if coming from Flex
                sup = supMap.get(ee.SupervisorId__c);
                //System.debug('supId__c***' + e.SupervisorId__c);
                emp = emps.get(ee.Id);
                if (sup != null) {
                    //System.debug('emp.Id: ' + emp.Id);
                    //System.debug(emp.Name);
                    emp.Supervisor__c = sup.Id;
                    //System.debug('sup id***' + emp.Supervisor__c);
                    supUpdates.add(emp);
                }
                else if (emp.Id != ceo.Id) {
                    emp.Supervisor__c = ceo.Id;
                    supUpdates.add(emp);
                }
            }        
        }
        if (!supUpdates.isEmpty()) update supUpdates;
    }
}
kathyanikathyani
I am not very clear. What did you mean if the origin point is trigger then the limit would be 20 and not 100