I have the below trigger which works perfectly but when I add an additional trigger, I get Error(System.LimitException: Too many SOQL queries: 101 ) which is really worrying me. Basically i can't add another trigger and when I combine the additional trigger with the initial one I still get the same error. So far from my research the error seems to be caused by putting an SOQL query inside a loop but mine doesn't have that so i'm not sure how to solve this. Any ideas?
trigger UpdateLatestRecords on Student_Records__c(after insert, after update, after delete) {
if( trigger.isinsert || trigger.isupdate ){
list<Id> accIdList = new list<id>();
list<Decimal> ExamValue = new list<Decimal>();
for(Student_Records__c c : trigger.new){
accIdList.add(c.Student__c);
}
list<Contact> acc = [
SELECT id, Latest_Overall_Marks__c
FROM Contact
WHERE id in: accIdList];
list<Student_Records__c> examrecords = [
SELECT position_in_class_F__c, Type_of_Exam__c, Overall_Marks__c, Name_of_school__c, Count_Number_of_Marks__c
FROM Student_Records__c
WHERE RecordTypeId in (
SELECT Id
FROM RecordType
WHERE Name = 'Student Exam Record')
AND (Type_of_Exam__c = 'Secondary End Term Exam' OR Type_of_Exam__c = 'Primary End Term Exam')
AND Student__c in: accIdList
ORDER BY Date__c DESC LIMIT 1];
list<Student_Records__c> KCPEexamrecords = [
SELECT position_in_class_F__c, Type_of_Exam__c, Overall_Marks__c, Name_of_school__c, Count_Number_of_Marks__c
FROM Student_Records__c
WHERE RecordTypeId in (
SELECT Id
FROM RecordType
WHERE Name = 'Student Exam Record')
AND Type_of_Exam__c = 'KCPE'
AND Student__c in: accIdList
ORDER BY Date__c DESC LIMIT 1];
list<Student_Records__c> KCSEexamrecords = [
SELECT position_in_class_F__c, Type_of_Exam__c, Overall_Marks__c, Name_of_school__c, Count_Number_of_Marks__c
FROM Student_Records__c
WHERE RecordTypeId in (
SELECT Id
FROM RecordType
WHERE Name = 'Student Exam Record')
AND Type_of_Exam__c = 'KCSE'
AND Student__c in: accIdList
ORDER BY Date__c DESC LIMIT 1];
list<Student_Records__c> feerecords = [
SELECT Amount__c, Date__c, Name_of_school__c
FROM Student_Records__c
WHERE RecordTypeId in (
SELECT Id
FROM RecordType
WHERE Name = 'Student Fees Record')
AND Type_of_fees__c = 'Tuition Fees'
AND Student__c in: accIdList
ORDER BY Date__c DESC LIMIT 1];
If(examrecords.size() == 0){
}
Else{
for(Contact a : acc){
a.Latest_Overall_Marks__c = examrecords[0].Overall_Marks__c;
a.Latest_Exam__c = examrecords[0].Type_of_Exam__c;
a.Latest_Position_in_Class__c = examrecords[0].position_in_class_F__c;
}
update acc;
System.debug('///ENDTERM CONTACT FIELDS updated///');
}
If(KCPEexamrecords.size() == 0){
}
Else{
for(Contact a : acc){
a.KCPE_Marks__c = KCPEexamrecords[0].Overall_Marks__c;
a.Primary_School__c = KCPEexamrecords[0].Name_of_school__c;
}
update acc;
System.debug('///KCPE CONTACT FIELDS updated///');
}
If(KCSEexamrecords.size() == 0){
}
Else{
for(Contact a : acc){
a.KCSE_Marks__c = KCSEexamrecords[0].Overall_Marks__c;
}
update acc;
System.debug('///KCSE CONTACT FIELDS updated///');
}
If(feerecords.size() == 0){
}
Else{
for(Contact a : acc){
a.Last_School_Fees__c = feerecords[0].Amount__c;
a.Current_School__c = feerecords[0].Name_of_school__c;
}
update acc;
System.debug('///FEES CONTACT FIELDS UPDATED///');
}
}
if( trigger.isdelete ){
list<Id> accIdList = new list<id>();
list<Decimal> ExamValue = new list<Decimal>();
for(Student_Records__c c : trigger.old){
accIdList.add(c.Student__c);
}
list<Contact> acc = [
SELECT id, Latest_Overall_Marks__c
FROM Contact
WHERE id in: accIdList];
list<Student_Records__c> examrecords = [
SELECT position_in_class_F__c, Type_of_Exam__c, Overall_Marks__c, Name_of_school__c, Count_Number_of_Marks__c
FROM Student_Records__c
WHERE RecordTypeId in (
SELECT Id
FROM RecordType
WHERE Name = 'Student Exam Record')
AND (Type_of_Exam__c = 'Secondary End Term Exam' OR Type_of_Exam__c = 'Primary End Term Exam')
AND Student__c in: accIdList
ORDER BY Date__c DESC LIMIT 1];
list<Student_Records__c> KCPEexamrecords = [
SELECT position_in_class_F__c, Type_of_Exam__c, Overall_Marks__c, Name_of_school__c, Count_Number_of_Marks__c
FROM Student_Records__c
WHERE RecordTypeId in (
SELECT Id
FROM RecordType
WHERE Name = 'Student Exam Record')
AND Type_of_Exam__c = 'KCPE' AND Student__c in: accIdList
ORDER BY Date__c DESC LIMIT 1];
list<Student_Records__c> KCSEexamrecords = [
SELECT position_in_class_F__c, Type_of_Exam__c, Overall_Marks__c, Name_of_school__c, Count_Number_of_Marks__c
FROM Student_Records__c
WHERE RecordTypeId in (
SELECT Id
FROM RecordType
WHERE Name = 'Student Exam Record')
AND Type_of_Exam__c = 'KCSE' AND Student__c in: accIdList
ORDER BY Date__c DESC LIMIT 1];
list<Student_Records__c> feerecords = [
SELECT Amount__c, Date__c, Name_of_school__c
FROM Student_Records__c
WHERE RecordTypeId in (
SELECT Id
FROM RecordType
WHERE Name = 'Student Fees Record')
AND Type_of_fees__c = 'Tuition Fees' AND Student__c in: accIdList
ORDER BY Date__c DESC LIMIT 1];
If(examrecords.size() == 0){
for(Contact a : acc){
a.Latest_Overall_Marks__c = NULL;
a.Latest_Exam__c = NULL;
a.Latest_Position_in_Class__c = NULL;
}
}
Else{
for(Contact a : acc){
a.Latest_Overall_Marks__c = examrecords[0].Overall_Marks__c;
a.Latest_Exam__c = examrecords[0].Type_of_Exam__c;
a.Latest_Position_in_Class__c = examrecords[0].position_in_class_F__c;
}
update acc;
System.debug('///ENDTERM CONTACT FIELDS updated///');
}
If(KCPEexamrecords.size() == 0){
}
Else{
for(Contact a : acc){
a.KCPE_Marks__c = KCPEexamrecords[0].Overall_Marks__c;
a.Primary_School__c = KCPEexamrecords[0].Name_of_school__c;
}
update acc;
System.debug('///KCPE CONTACT FIELDS updated///');
}
If(KCSEexamrecords.size() == 0){
}
Else{
for(Contact a : acc){
a.KCSE_Marks__c = KCSEexamrecords[0].Overall_Marks__c;
}
update acc;
System.debug('///KCSE CONTACT FIELDS updated///');
}
If(feerecords.size() == 0){
}
Else{
for(Contact a : acc){
a.Last_School_Fees__c = feerecords[0].Amount__c;
a.Current_School__c = feerecords[0].Name_of_school__c;
}
update acc;
System.debug('///FEES CONTACT FIELDS UPDATED///');
}
}
}