Changing email display names to custom names on force.com sites

If you are sending a custom email on force.com site using apex on a feature like forgot password or username, one of the challenges is to change the display name of the email. Even though apex has a setdisplayname method , it will not work on the guest users as salesforce always appends the site creator username name on the emails which is a pain.
Solution
a. If you need to send a custom email on apex for guest users, create a organziation wide email address and create an email id which should be accessible on all profiles.
b. Now on the apex code, query from organization address and set the organizationaddress id on the email object. Now all the emails would come up the display name used on the organization address.

List lstOrgWideEmailId = [Select id from OrgWideEmailAddress];
if(lstOrgWideEmailId.size() == 0)
{
throw(new PkException('There is no Organization wide email address setup in the org. Please set the organization wide email address'));
}else
{
orgWideEmailAddressID = lstOrgWideEmailId[0].id;
}
Messaging.Singleemailmessage email = new Messaging.Singleemailmessage();

email.setReplyTo(fromaddress);
email.setOrgWideEmailAddressId(orgWideEmailAddressID);

Leave a Reply

Your email address will not be published. Required fields are marked *

Get free tips on Salesforce
Get free tips on Salesforce
We respect your privacy.