1

I have an after update trigger on the Case object that calls a static class method to send a SingleEmailMessage to a user with the case details. That method is rendering a stored VisualForce email template. When a case is updated by a Salesforce user, everything runs properly and the email is sent. When a case is updated by a Partner Community User, the transaction fails due to an exception:

System.EmailTemplateRenderException: Unable to retrieve object

The error is being caused by this line:

Messaging.SingleEmailMessage email = Messaging.renderStoredEmailTemplate(templateId, userId, caseId);

Here is my full code:

Apex Trigger

trigger CaseEmailNotificationTrigger on Case (after update) {
Map<Id, Case> oldCaseMap = new Map<Id, Case>();
List<Case> newCaseList = new List<Case>();
List<Case> casesToSendAssignedNotification = new List<Case>();
List<Case> casesToSendDirectToLiveSiteNotification = new List<Case>();
List<Case> casesToSendEscalatedNotification = new List<Case>();
CaseSettings__c caseSettings = CaseSettingsHelper.getCaseSettings();

for (Case oldCase : Trigger.old) {
    oldCaseMap.put(oldCase.Id, oldCase);
}
for (Case newCase : Trigger.new) {
    newCaseList.add(newCase);
}
if (newCaseList.size() > 0) {
    for (Case newCase : newCaseList) {
        Case oldCase = oldCaseMap.get(newCase.Id);
        if (oldCase.OwnerId != newCase.OwnerId || oldCase.Assigned_Action__c != newCase.Assigned_Action__c) {
            casesToSendAssignedNotification.add(newCase);
        }
        if (oldCase.Change_Made_Directly_on_Live_Site__c == false && newCase.Change_Made_Directly_on_Live_Site__c == true) {
            casesToSendDirectToLiveSiteNotification.add(newCase);
        }
        if (oldCase.IsEscalated == false && newCase.IsEscalated == true) {
            casesToSendEscalatedNotification.add(newCase);
        }
    }
}
if (casesToSendAssignedNotification.size() > 0) {
    for (Case caseToEmail : casesToSendAssignedNotification) {
        CaseEmailNotificationHelper.sendUserNotification(caseSettings.Assigned_Email_Template_Name__c, caseToEmail.Id, caseToEmail.OwnerId);
    }
}
if (casesToSendDirectToLiveSiteNotification.size() > 0) {
    for (Case caseToEmail : casesToSendDirectToLiveSiteNotification) {
        CaseEmailNotificationHelper.sendCaseTeamNotification(caseSettings.Direct_to_Live_Site_Email_Template_Name__c, caseToEmail.Id, true);
    }
}
if (casesToSendEscalatedNotification.size() > 0) {
    for (Case caseToEmail : casesToSendEscalatedNotification) {
        CaseEmailNotificationHelper.sendCaseTeamNotification(caseSettings.Escalated_Email_Template_Name__c, caseToEmail.Id, true);
    }
}
}

Helper Class

public without sharing class CaseEmailNotificationHelper {
public static List<Messaging.EmailFileAttachment> getFileAttachments(Id caseId) {
    Attachment[] caseAttachments = [SELECT Id, Name, Body FROM Attachment WHERE ParentId = :caseId];
    List<Messaging.EmailFileAttachment> fileAttachments = new List<Messaging.EmailFileAttachment>();
    if (caseAttachments.size() > 0) {
        for (Attachment attachment : caseAttachments) {
            Messaging.EmailFileAttachment fileAttachment = new Messaging.EmailFileAttachment();
            fileAttachment.setFileName(attachment.Name);
            fileAttachment.setBody(attachment.Body);
            fileAttachments.add(fileAttachment);
        }
    }
    return fileAttachments;
}

public static Messaging.SingleEmailMessage getEmail(String templateName, Id userId, Id caseId, Boolean highPriority) {
    CaseSettings__c caseSettings = CaseSettingsHelper.getCaseSettings();
    Id templateId = [SELECT Id FROM EmailTemplate WHERE DeveloperName = :templateName LIMIT 1].Id;
    Messaging.SingleEmailMessage email = Messaging.renderStoredEmailTemplate(templateId, userId, caseId);
    if (caseSettings.Org_Wide_Email_Address__c != null) {
        Id orgWideEmailAddressId = [SELECT Id FROM OrgWideEmailAddress WHERE Address = :caseSettings.Org_Wide_Email_Address__c].Id;
        email.setOrgWideEmailAddressId(orgWideEmailAddressId);
    }
    if (caseSettings.Reply_To_Email_Address__c != null) {
        email.setReplyTo(caseSettings.Reply_To_Email_Address__c);
    }
    if (highPriority == true) {
        email.emailPriority = 'Highest';
    }
    List<Messaging.EmailFileAttachment> fileAttachments = CaseEmailNotificationHelper.getFileAttachments(caseId);
    if (fileAttachments.size() > 0) {
        email.setFileAttachments(fileAttachments);
    }
    return email;
}

public static void sendCaseTeamNotification(String templateName, Id caseId, Boolean highPriority) {
    CaseSettings__c caseSettings = CaseSettingsHelper.getCaseSettings();
    List<String> recipientEmails = new List<String>();
    Id groupId = [SELECT Id FROM Group WHERE DeveloperName = :caseSettings.Change_Request_Team_Group_Name__c].Id;
    GroupMember[] changeRequestTeamGroupMembers = [SELECT UserOrGroupId FROM GroupMember WHERE GroupId = :groupId];
    List<Id> changeRequestTeamUserIds = new List<Id>();
    for (GroupMember member : changeRequestTeamGroupMembers) {
        changeRequestTeamUserIds.add(member.UserOrGroupId);
    }
    User[] changeRequestTeamUsers = [SELECT Email FROM User WHERE Id IN :changeRequestTeamUserIds];
    for (User u : changeRequestTeamUsers) {
        recipientEmails.add(u.Email);
    }
    if (recipientEmails.size() > 0) {
        Messaging.SingleEmailMessage email = CaseEmailNotificationHelper.getEmail(templateName, UserInfo.getUserId(), caseId, highPriority);
        email.setToAddresses(recipientEmails);
        Messaging.SendEmailResult [] result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
    }
}

public static void sendUserNotification(String templateName, Id caseId, Id userId) {
    Messaging.SingleEmailMessage email = CaseEmailNotificationHelper.getEmail(templateName, userId, caseId, false);
    String recipientEmail = [SELECT Email FROM User WHERE Id = :userId].Email;
    email.setToAddresses(new List<String>{recipientEmail});
    Messaging.SendEmailResult [] result = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}

Stack Trace

Class.CaseEmailNotificationHelper.getEmail: line 19, column 1 Class.CaseEmailNotificationHelper.sendUserNotification: line 58, column 1 Trigger.CaseEmailNotificationTrigger: line 31, column 1

1

As per documentation provided in the below link

Explanation

it says

For partner users to use email templates, you must make the templates available to them.

So could you please double check it.

| improve this answer | |
  • I saw that, but I'm not sure how to explicitly make templates available to partner community users. Also, the debug logs show that the email template is being retrieved from the database. I'm guessing the error message is referring to the case object or the user object not being able to be retrieved when the email template is being rendered. – jfreeman Mar 31 '17 at 13:15
0

Turns out the error message was referring to the user object. I guess the Partner Community license type prevents the email template from retrieving a Salesforce license User object. I changed this line:

Messaging.SingleEmailMessage email = CaseEmailNotificationHelper.getEmail(templateName, userId, caseId, false);

to:

Messaging.SingleEmailMessage email = CaseEmailNotificationHelper.getEmail(templateName, UserInfo.getUserId(), caseId, false);

That solved the problem.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.