I am facing Too many SOQL Error. Reason: Using "renderStoredEmailTemplate" method inside for loop that counts against SOQL query for every iteration.
global class FlowInput {
@InvocableVariable
global List<id> UserId;
@InvocableVariable
global Support_Request__c getSR;
@InvocableVariable
global String EmailTemplate;
}
Sample Code:
//1st Param: record Id and Template Id
//2nd Param - List of WrapperObject containing record id and multiple user ids for the corresponding record id
global static void SendEmailMessage(map<ID, ID> SRIdemailTemplateId, List<FlowInput> varFlowInput){
List<Messaging.SingleEmailMessage> msgList = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail;
for(FlowInput SRandTeamMemberwrapper : varFlowInput){
for(Id userId : SRandTeamMemberwrapper.UserId){
System.debug('4.Number of Queries used in this apex code so far: ' + Limits.getQueries());
mail = Messaging.renderStoredEmailTemplate(SRIdemailTemplateId.get(SRandTeamMemberwrapper.getSR.Id), userId, SRandTeamMemberwrapper.getSR.Id);
mail.setTargetObjectId(userId);
mail.saveAsActivity = false;
msgList.add(mail);
}
}
Messaging.sendEmail(msgList);
Integer remainingInvocations = Limits.getLimitEmailInvocations() - Limits.getEmailInvocations();
System.debug('remainingInvocations=='+remainingInvocations);
In future will be brining Batch apex. But again I do not want to have this method to be put inside nested for loop. The reason for having nested for loop is to bring the email template based upon the type of request. Any idea if there is any work around to avoid CPU limit.