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
sp13sp13 

Trigger System.LimitException: Too many SOQL queries: 101

hi, why is it that my trigger shows this error = Error:Apex trigger SSExpensesCountonPayroll caused an unexpected exception, contact your administrator: SSExpensesCountonPayroll: System.LimitException: Too many SOQL queries: 101?
what should i do to prevent this error?

this is my trigger:

trigger SSExpensesCountonPayroll on Expense__c (after insert, after update, after delete) {
    Map<Id, List<Expense__c>> PayrollExpenses = new Map<Id, List<Expense__c>>();
    Set<Id> PRIds = new Set<Id>();   
   
    List<Payroll__c> PRList = new List<Payroll__c>();
    List<Expense__c> ExList = new List<Expense__c>();
   
    if(trigger.isInsert || trigger.isUPdate) {
        for(Expense__c ex : trigger.New) {
            PRIds.add(ex.Payroll__c);    
        } 
    }
   
    if(trigger.isDelete) {
        for(Expense__c ex : trigger.Old) {
            PRIds.add(ex.Payroll__c);    
        } 
    }      
   
    ExList = [SELECT Id, Amount__c FROM Expense__c WHERE Type_of_Expense__c = 'Spareparts & Supplies' and Payroll__c IN : PRIds];
    double t=0;
    for(Expense__c ex : ExList) {
            if(ex.Amount__c != null) {
                t += ex.Amount__c;
            } 
    }   
       
    PRList = [SELECT Total_SS_Expense__c FROM Payroll__c WHERE Id IN : PRIds];
   
    for(Payroll__c pr : PRList) {
        List<Expense__c> ExpList = new List<Expense__c>();
        ExpList = PayrollExpenses.get(pr.Id);
        pr.Total_SS_Expense__c = t;
    }   
    update PRList;
}

Best Answer chosen by sp13
Abhi_TripathiAbhi_Tripathi
Hi,

I think its not your whole code, Somewhere you must be updating Expense__c , by which your trigger get called again and makes it too many SOQL , because of which you get this error.