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
mohan s 37mohan s 37 

AggregateResult query too many soql 101 exception

Hi All, 
         I am getting the to many soql 101 exception in the below class. How do i overcome this issue. Can any one help me to resolve this issue.
Trigger childafteraction on Child__c(after insert, after update){
Set<Id> ids = new Set<id>();
if(trigger.isInsert || trigger.isUpdate){
for(Child__c chlds{
new UpdateStatus().updateParent(ids);
public class UpdateStatus {
    public void updateParent(Set<Id> InputIds){
        Map<Id,Integer> parentMap = new Map<Id,Integer>();
        Map<Id,Integer> mapToCompare = new Map<Id,Integer>();
        Set<Id> idsToUpdate = new Set<Id>();
        for(Parent_Obj__c p:[SELECT id,Name, Status__c,Paid__c,(select id,name,Parent__c,Amount_Paid__c from RelationShipName where Parent__c IN:InputIds ) FROM Parent_Obj__c WHERE Id IN:InputIds]){ // here I am getting too many soql 101 exception
             parentMap.put(p.Id, p.RelationshipName__r.size()); 
        for(AggregateResult result: [SELECT COUNT(Id), Parent__c FROM Child__c WHERE Amount_Paid__c=true AND Parent__c IN:InputIds GROUP BY Parent__c ]){ // here also i am getting too many soql 101 exception.
            Id pId = ((Id)result.get('Parent__c'));
            Integer count = ((Integer)result.get('expr0'));
        for(Id iterateIds :mapToCompare.keySet()){
            if(parentMap.containsKey(iterateIds) && parentMap.get(iterateIds) == mapToCompare.get(iterateIds)){
        List<Parent__c> finalListToUpdate = new List<Parent__c>();
        for(Parent__c pIterate : [SELECT Id,Name,Status__c FROM Parent_Obj__c WHERE Id IN:idsToUpdate]){
           pIterate.Status__c = 'paid';
           Database.SaveResult[] result =database.update(finalListToUpdate,false);
Hi Mohan,

To many SOQL 101 error comes when you do more than 100 query in a single transaction, As i checked ur code it is fine becuase you are not doing query inside a loop, another thing you can check is that updating parentlist will initiate parent triggers to run so check there if you doing something there which cause this child trigger to run and run again , and also please check process builder and flows which cause this child trigger to run?

Saket Sharma
Pradeep SinghPradeep Singh

You are using UPDATE DML in after update trigger. There might be some trigger on parent object which is updating the child records and the trigger is running again which causes the recursion.

Please add this to your class :-
public static Boolean run = true;
and the below one to your trigger:-

if( ){ = false;

Hope this helps you..!!!
mohan s 37mohan s 37
Hi Saket Sharma,

                             Thank you for your input on this issue. I have specified the limit on the query then it's working fine.