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
Jess RobinsonJess Robinson 

System.LimitException: Maximum stack depth reached: 5 on Database.query

Hi all, 
I am seeing this exception: System.LimitException: Maximum stack depth reached: 5
on a Database.query call in a scheduled job. Have tried the job running as Scheduled, Asynchronous and Queueable and it happens in every instance, but only intermittently. Could anyone shed any light on what might be causing this? 

Code:
string soqlString = 'select count(Id) activeCount, OwnerId from '+team.Object_name__c+' where '+
                (team.Object_name__c == 'Lead' ? 'IsConverted = false and ' : '') +
                'OwnerId IN :memberUserIdsList and '+team.Object_active_status_field_name__c+' IN :statusValueList'+
                ' group by OwnerId';
      for (sObject sObj : Database.query(soqlString)) { <-- Exception occurring here
        AggregateResult ar = (AggregateResult)sObj;
        id memberOwnerId = (string)ar.get('OwnerId');
        integer activeObjectCount = (integer)ar.get('activeCount');
        Team_member__c teamMember = memberByUserIdMap.get(memberOwnerId);
        teamMember.Number_of_active_objects__c = activeObjectCount;
      }


 
Akhil ReddyAkhil Reddy

 

I suspect your IN clause is the problem here, please review the below limits

please refer to 

https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_comparisonoperators.htm
Jess RobinsonJess Robinson
I'm afraid I don't follow - I can't find any limits in that page that would apply to my query. We are not using any sub-queries or semi-joins.