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
sanju21sanju21 

Too many SOQL queries exception even when the query is outside for loop

I am not understanding why I am getting this exception when I run tests on the apex test class that is related to a trigger. The exception is being thrown at the following line(where I am querying for total child cases) in trigger when I 'Run tests' on the respective test class. Any help is very much appreciated.

 

List<Case> parToTotalChildsCases = [Select Id, Status, parentID from Case where ParentId IN :offParList and IsDeleted = false];
    
    // get total child cases for each parent
    for (case c: parToTotalChildsCases){
    	system.debug('child ID in total==='+c.Id);
    	parToTotalChilds.put(c.parentId, totalCount++);
    	
    }

 

bob_buzzardbob_buzzard

Its still possible to get this error with the SOQL outside a loop - its SOQL queries within the entire transaction.  Do you have lots of SOQL elsewhere?

sanju21sanju21

Thanks Bob for the prompt reply. I have lot of SOQL queries in the same trigger. The trigger works perfectly fine when I work on a record in SF, its just when I run tests, its failing..should I disable the line its failing in test or should I look into the whole test or should I look/rewrite into the whole trigger..can it throw exception when I work in SF too because its failing in the tests?

bob_buzzardbob_buzzard

If it can throw an exception in test then its technically possible it will throw an exception in production.  It really depends on what you are doing and whether the test is based on a real business case or is trying to break the code.

phantom1982phantom1982