You need to sign in to do that
Don't have an account?
Joseph Bauer
Apex Email Not Working When Changing Subject and/or Body
I'm experiencing some very weird behavior with SingleEmailMessage. I'm just trying to send an email to all users in a case team on the case. It works if I hard code values for subject and body. I want to use variables. But whats even stranger is it doesn't work if I change the hardcoded values. For example if I change the subject from 'Test Message' to 'Case Updated' then it doesn't send me the email. Why would changing the subject prevent the email from being sent? Same thing is happening with the body... Here is my code. Can someone reproduce this issue or is it just my org?
So the above code works, but if I change the Test Message subject to Case Updated it doesn't work.
public class CaseHistoryTrackerHandler { public static Messaging.Email[] allMails{get;} public static List<Id> userIds{get;set;} static { userIds = new List<Id>(); } public static void handleCaseHistory(List<Case> newCases, Map<Id, Case> oldCaseMap) { final List<Schema.FieldSetMember> trackedFields = SObjectType.Case.FieldSets.HistoryTracking.getFields(); if (trackedFields.isEmpty()) return; Map<Id, List<Id>> caseIdToUserIds = getCaseUsers(newCases); Map<Id, User> userIdToUser = new Map<Id, User>([SELECT Email FROM User WHERE Id IN :userIds]); Messaging.SingleEmailMessage[] messages = new List<Messaging.SingleEmailMessage>(); for (Case newCase : newCases) { final Case oldCase = oldCaseMap.get(newCase.Id); final List<Id> userIdsForEmail = caseIdToUserIds.get(newCase.Id); if(userIdsForEmail == null || userIdsForEmail.size() == 0) return ; String body = 'Case id: ' + newCase.Id + ' has been modified by the following changes: <br />'; for (Schema.FieldSetMember fsm : trackedFields) { String fieldName = fsm.getFieldPath(); String fieldLabel = fsm.getLabel(); if (newCase.get(fieldName) == oldCase.get(fieldName)) continue; String oldValue = String.valueOf(oldCase.get(fieldName)); String newValue = String.valueOf(newCase.get(fieldName)); if (oldValue != null && oldValue.length()>255) oldValue = oldValue.substring(0,255); if (newValue != null && newValue.length()>255) newValue = newValue.substring(0,255); body += 'Field: ' + fieldLabel + '<br />'; body += 'Changed By: ' + UserInfo.getUserId() + '<br />'; body += 'Old Value: ' + oldValue + '<br />'; body += 'New Value: ' + newValue + '<br /><br />'; } system.debug(body); // create email for each user for(Id userId : userIdsForEmail) { Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage(); message.toAddresses = new String[] { userIdToUser.get(userId).email }; message.subject = 'Test Message'; message.setHtmlBody('Test'); messages.add(message); } } Messaging.SendEmailResult[] results = Messaging.sendEmail(messages); if (results[0].success) { System.debug('The email was sent successfully.'); } else { System.debug('The email failed to send: ' + results[0].errors[0].message); } } public static Map<Id, List<Id>> getCaseUsers(List<Case> newCases) { Map<Id, List<Id>> caseIdToUserIds = new Map<Id, List<Id>>(); for(CaseTeamMember ctm : [SELECT MemberId, ParentID FROM CaseTeamMember WHERE ParentID IN :newCases]){ if(caseIdToUserIds.containsKey(ctm.ParentID)) caseIdToUserIds.get(ctm.ParentID).add(ctm.MemberID); else { caseIdtoUserIds.put(ctm.ParentID, new List<Id>{ctm.MemberID}); } userIds.add(ctm.MemberID); system.debug('added new member: ' + ctm.MemberID); } for(Case newCase : newCases) { system.debug(caseIdToUserIds.get(newCase.Id)); } return caseIdToUserIds; } }
So the above code works, but if I change the Test Message subject to Case Updated it doesn't work.