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
MG ConsultingMG Consulting 

Unexpected Too Many SOQL Queries Exception

Hi,

 

I'm only counting 5 SOQL queries in the code below and yet I received the error below. So far as I can tell, none of my 5 queries are in a loop, so I can't figure out how they could possible execute 21 times before hitting the first one...

 

I'm guessing that maybe the context of the governor limits are larger then the trigger itself, but I really have no idea what is going on...

 

NewEmailMessage: execution of BeforeUpdate

caused by: System.Exception: Too many SOQL queries: 21

Trigger.NewEmailMessage: line 12, column 42

 

trigger NewEmailMessage on Case (before update) {
Set<Id> caseIds = new Set<Id>();
for (Case c: Trigger.new) {
if (c.NewEmailMessage__c == true) {
caseIds.add(c.Id);
c.NewEmailMessage__c = false;
}
}

if (caseIds.size() != 0) {
Set<Id> taskIds = new Set<Id>();
for (EmailMessage emailMessage : [SELECT ActivityId
FROM EmailMessage
WHERE ParentId IN :caseIds]) {
taskIds.add(emailMessage.ActivityId);
}

Map<Id, Task> tasks = new Map<Id, Task>([SELECT Id
FROM Task
WHERE Id IN :taskIds]);

String[] fromAddresses = new String[]{};
for (EmailMessage emailMessage : [SELECT FromAddress
FROM EmailMessage
WHERE Incoming = true
AND ActivityId IN :tasks.keySet()]) {
if (emailMessage.FromAddress != null)
fromAddresses.add(emailMessage.FromAddress);
}

Map<String, Id> emailContactId = new Map<String, Id>();
if (fromAddresses.size() != 0) {
for(Contact contact : [SELECT Id, Email
FROM Contact
WHERE Email IN :fromAddresses
ORDER BY CreatedDate DESC]) {
if (contact.Email != null)
emailContactId.put(contact.Email, contact.Id);
}
}

CaseCommentExtension__c[] caseCommentExtensions = new CaseCommentExtension__c[]{};
for (EmailMessage emailMessage : [SELECT ParentId,
Incoming,
FromAddress,
FromName,
ToAddress,
CcAddress,
BccAddress,
Subject,
TextBody
FROM EmailMessage
WHERE ActivityId IN :tasks.keySet()]) {
CaseCommentExtension__c caseCommentExtension = new CaseCommentExtension__c();
caseCommentExtension.ParentId__c = emailMessage.ParentId;
caseCommentExtension.IsPublished__c = true;
caseCommentExtension.Tier__c = 'Customer';

if (emailMessage.Incoming == true) {
caseCommentExtension.Type__c = 'Inbound E-mail';

if (emailContactId.containsKey(emailMessage.FromAddress))
caseCommentExtension.Contact__c = emailContactId.get(emailMessage.FromAddress);
}
else
caseCommentExtension.Type__c = 'Outbound E-mail';
caseCommentExtension.Subject__c = emailMessage.Subject;

String commentBody = '';
if (emailMessage.FromAddress != null)
commentBody += 'From Address: ' + emailMessage.FromAddress + '\n';
if (emailMessage.FromName != null)
commentBody += 'From Name: ' + emailMessage.FromName + '\n';
if (emailMessage.ToAddress != null)
commentBody += 'To Address: ' + emailMessage.ToAddress + '\n';
if (emailMessage.CcAddress != null)
commentBody += 'CC Address: ' + emailMessage.CcAddress + '\n';
if (emailMessage.BccAddress != null)
commentBody += 'BCC Address: ' + emailMessage.BccAddress + '\n';
if (emailMessage.TextBody != null)
commentBody += 'Text Body:\n' + emailMessage.TextBody;
caseCommentExtension.CommentBody__c = commentBody;

Trigger.newMap.get(emailMessage.ParentId).NewestComment__c = commentBody;

caseCommentExtensions.add(caseCommentExtension);
}

delete tasks.values();
insert caseCommentExtensions;
}
}

 

Thanks for the help,

Mike

 

 

WhyserWhyser

Any APEX code you have that may activate your trigger will also count towards the governer limits.

So say for instance you are writing test code that does a mass change on Cases, it will run your trigger multiple times in that case.

Anyways that's just one idea, I don't know if that helps. It may be something else. Do you have anything that updates cases after this trigger runs?