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);

Please Subscribe

Subscribe to our mailing list to get tips on maximizing your salesforce

We respect your privacy.

Please subscribe

Subscribe to our mailing list and get tips to maximize salesforce to your email inbox.

I am honored to have your subscription. Stay tuned for tips to maximize your salesforce investment

Something went wrong.

buyan47

Author: buyan47

Hi there! My name is Buyan Thyagarajan. I am a Salesforce consultant specializing in Higher Education, Manufacturing and Marketing Automation. My blogs will help you to maximize your Salesforce CRM investments, prevent problems beforehand and make the right decisions. If you need to talk to me right away, you can email me at buyan47@gmail.com or call me at 302-438-4097

Leave a Reply

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