Discovered this recently because a trigger was failing when triggered by a community user. Can't figure out why this should matter though, with the apex running in system mode and all. Is this specific to the Messaging class in some way?
trigger FeedItemTrigger on FeedItem (after insert) {
if(Trigger.isInsert && Trigger.isAfter){
Set<Id> setOppId = new Set<Id>();
Map<Id,FeedItem> mapOppIdToFeedItemId = new Map<Id,FeedItem>();
for(FeedItem f : Trigger.New) {
mapOppIdToFeedItemId.put(f.ParentId,f);
}
List<Opportunity> oppToUpdate = new List<Opportunity>();
List<Opportunity> oppList = [SELECT Last_Chatter_Post__c FROM Opportunity WHERE Id IN: mapOppIdToFeedItemId.keySet()];
for(Opportunity opp : [SELECT Last_Chatter_Post__c FROM Opportunity WHERE Id IN: mapOppIdToFeedItemId.keySet()]){
FeedItem item = mapOppIdToFeedItemId.get(opp.Id);
String items = item.Body.replaceAll('<[/a-zAZ0-9]*>','');
items += '\n Created By: '+ UserInfo.getFirstName() + ' ' +UserInfo.getLastName();
opp.Last_Chatter_Post__c = items;
oppToUpdate.add(opp);
}
update oppToUpdate;
List<String> toAddresses = new List<String>();
List<Feed_Item_Setting__mdt> recipientList = [SELECT MasterLabel, QualifiedApiName, User_Email__c FROM Feed_Item_Setting__mdt Limit 200];
for (Feed_Item_Setting__mdt recipient : recipientList) {
if(recipient.User_Email__c != null){
toAddresses.add(recipient.User_Email__c);
}
}
//Stores user email / Id
Map<String, User> userEmailIdMap = new Map<String, User>();
if(toAddresses.size() >0){
try{
for(User u : [Select Email, FirstName, LastName, Id From User where Email IN: toAddresses]){
if(!userEmailIdMap.containsKey(u.Email)){
userEmailIdMap.put(u.Email, u);
}
}
}catch(Exception e){
system.debug('Error getting user info ' + e.getMessage());
}
}
//Get email template name
List<Email_Template__mdt> emailTemplateName = [SELECT MasterLabel, QualifiedApiName FROM Email_Template__mdt Where MasterLabel = 'Feed Item' Limit 1];
EmailTemplate objCaseTemplate;
EmailTemplate objTemplate;
if(emailTemplateName.size() > 0){
for(EmailTemplate objTemp : [SELECT Id, Subject, HtmlValue, Body, DeveloperName FROM EmailTemplate WHERE (DeveloperName =: emailTemplateName[0].QualifiedApiName OR DeveloperName = 'ChatterPostNotifyVF_Case')]){
if(objTemp.DeveloperName == emailTemplateName[0].QualifiedApiName){
objTemplate = objTemp;
}else{
objCaseTemplate = objTemp;
}
}
}
for(FeedItem f : Trigger.New) {
//Create email
List<Messaging.SingleEmailMessage> theEmails = new List<Messaging.SingleEmailMessage>();
for(String email : toAddresses){
if(userEmailIdMap.containsKey(email)){
//Create email
Messaging.SingleEmailMessage mail;
System.debug('>>>>>>'+objTemplate);
System.debug('>>>>>>'+objCaseTemplate);
if(objTemplate != null || objCaseTemplate != null){
//template id, whoId, WhatId
String templateId = (String.valueOf(f.parentId).startsWith('500')?objCaseTemplate.Id:objTemplate.Id);
mail = Messaging.renderStoredEmailTemplate(templateId, userEmailIdMap.get(email).Id, f.Id);
if(!String.valueOf(f.parentId).startsWith('500'))
mail.setToAddresses(new List<String>{'[email protected]'});
}else{
mail = new Messaging.SingleEmailMessage();
mail.setHtmlBody(f.body);
mail.setSubject('New Chatter Post');
}
//The ID of the contact, lead, or user to which the email will be sent
//Specifying the user ID in setTargetObjectId means the email doesn’t count toward the daily limit.
mail.setTargetObjectId(userEmailIdMap.get(email).Id);
//saveAsActivity must be false when sending mail to users
mail.setSaveAsActivity(false);
theEmails.add(mail);
}
}
String emailErrorReport = '';
//Send Email
if(!Test.isRunningTest()){
System.debug('>>>>>>theEmails>>>>>'+theEmails);
Messaging.SendEmailResult[] results = Messaging.sendEmail( theEmails );
for( Messaging.SendEmailResult currentResult : results ) {
for( Messaging.SendEmailError currentError : currentResult.getErrors() ) {
emailErrorReport = emailErrorReport + '(' + currentError.getStatusCode() + ') ' + currentError.getMessage() + '\r' ;
}
}
}
if(!String.isEmpty(emailErrorReport)){
system.debug('Email Error Report ---> ' + emailErrorReport);
}
}
}
}