You need to sign in to do that
Don't have an account?

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;
}
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.