I am calling sendemail() from visualforce.There are no SOQL Queries inside for loops but am getting error in controller.
If I comment below two lines then am not getting error
Messaging.SingleEmailMessage email = Messaging.renderStoredEmailTemplate(et.id, c.Id, ServiceId);
t.Description = 'Email Body:'+'\n'+email.getPlainTextBody();
Too many SOQL queries: 101
There are 120 Contacts in the ContactList.Debug log SOQL Limit @@@@ inside for loop starts from 3.
<apex:page standardController="Service__c"
extensions="EmailLetterExtension" showHeader="true" Sidebar="true"
action="{!sendEmail}"> Thank you! Email is sent.
</apex:page>
public void sendEmail(){
List<contact> lstcon=[Select Id,Name,Email,RecordTypeId,RecordType.Name From Contact where RecordType.Name = 'Service'];
List<Id> lstids= new List<Id>();
for(Contact c:lstcon)
{
lstids.add(c.id);
}
EmailTemplate et=[Select id,subject,body from EmailTemplate where name = 'Service Template' limit 1];
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTargetObjectIds(lstIds);
mail.setTemplateId(et.id);
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.MassEmailMessage[] { mail });
list<Task> tList = new list<Task>();
for(Contact c:lstcon){
Task t = new Task();
t.whatId = ChauffeurId;
t.WhoId = c.id;
t.Subject = 'Email : Letter sent to '+ c.Name + ' on '+ datetime.now().format('MM/dd/YYYY');
Messaging.SingleEmailMessage email = Messaging.renderStoredEmailTemplate(et.id, c.Id, ServiceId);
t.Description = 'Email Body:'+'\n'+email.getPlainTextBody();
t.status = 'Completed';
t.Priority = 'Normal';
t.ActivityDate = system.today();
tList.add(t);
system.debug('SOQL Limit @@@@' +Limits.getQueries());
}
insert tList;
}
sendEmail()
via a command button on your visualforce page, then this method cannot be the source of your query limit error. Providing the entire text of the error message verbatim would help. Part of the error message should be a stack trace, which should help you narrow down the issue to a few classes or triggers. YoursendEmail()
method is likely part of that stack trace, but it's probably at the very bottom of it. Look more towards the top of the stack trace.