+ Start a Discussion
RR MRR M 

Error: System.LimitException: Too many SOQL queries: 101 Trigger.RollupCountOnLoans: line 28, column 1

Hi,
Please Help to resolve this issue. Error: System.LimitException: Too many SOQL queries: 101 Trigger  line 28, column 1


Trigger RollupCountOnLoans on Loan__c(after insert, after update) {
    List <Id> Ids = new List<Id> ();
   List <Customer__c> Cust1 = new List<Customer__c>();
    List <AggregateResult> Agr = new List<AggregateResult>();
    
    for(Loan__c req:trigger.new){
        Ids.add(req.Customer_ID__c);
    }
    
    List<Customer__c> Cust = [Select Id, Number_of_Approved_Loans__c From Customer__c Where Id In :Ids];
    Agr = [Select Customer_ID__c, Count(Id) From Loan__c Where Customer_ID__c IN: Ids
            AND Customer_Journey_Status__c IN ('Approved','Active', 'PaidOff')
            Group By Customer_ID__c];
            
            for(AggregateResult ar: Agr){
        for(Customer__c p:Cust){
            if(ar.get('Customer_ID__c') == p.Id)
            {
               p.Number_of_Approved_Loans__c = Decimal.ValueOf(String.ValueOf(ar.get('expr0')));
            }
        }
    }
    update(Cust);
}
Kai Herng LauKai Herng Lau
Hi,

Do you have trigger listen on Customer__c update?
RR MRR M

Perent object :  Customer__c 

Child Object : Loan__c

NForceNForce
Hi Rr,
Seems like Loan trigger is bulkified but not the customer trigger.
Check for queries in loops in customer trigger. 

Thanks
RR MRR M
Can you please help with modify code am newbie in Development. 
NForceNForce
We don't know what is in Customer trigger that causes 101 error.