I would know if it's possible set a "User" for an email template.
In a my email template I use {!User.Name}
.
I have a class and a trigger on CollaborationGroupMember and I send an email each time that a user is added in a chatter group:
Class
public with sharing class ClassToSendMail {
public static void sendMail(String emailTemplate, String mailRecipient, Id targetObjectID) {
Id templateId;
templateId = [select id, name from EmailTemplate where developername = : emailTemplate].id;
OrgWideEmailAddress owa = [select id, Address, DisplayName from OrgWideEmailAddress limit 1];
Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
String[] toRecipients = new String[]{'[email protected]'};
email.setToAddresses(toRecipients);
email.setOrgWideEmailAddressId(owa.id);
email.setTargetObjectId(targetObjectID);
email.setTemplateId(templateId);
email.setSaveAsActivity(false);
try
{
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
catch (EmailException e) {String errorMsg='Error in : '+e.getMessage();}
}
}
Trigger
trigger TriggerSendMail on CollaborationGroupMember (after insert) {
for(CollaborationGroupMember newMember : Trigger.new){
System.debug('@@@ newMember: ' + newMember);
User userToSendMail = [SELECT id, Email FROM User WHERE ID=:newMember.MemberID];
ClassToSendMail.sendMail('UserAddedGroup', userToSendMail.Email , userToSendMail.Id);
}
}
When I add an user in a chatter group, {!User.Name}
is replace with name of the current user logged in org and not the user set as targetObject.
{!Receiving_User.Name}
instead of{!User.Name}
?