Automating the selection of Org Wide Addresses

Org Wide Address (OWA) is a great new feature recently added to which allows you to share a common email alias for users of a profile. This feature is particularly helpful for customer's using the application for managing support cases and, as such, need a common from email address like 

I was recently involved with a customer who's business involves the management of outsourced support centers who wanted to look at how they could leverage OWA to achieve their requirements. In particular, the support agents often managed requests from multiple customers where the agents had to respond with a particular company's email. 

To make this more clear, lets take the scenario where Fred Smith, an agent for, a company who provides outsourced support centers for the following 3 companies:,, Whenever a customer sends an email to (or one of the other fictitious companies) and is received by Fred, all of his emails (from address) must come from The seamless user experience is important to's success as an outsourcer.

In the above example, Fred has to manage three customers seamlessly, and has the three org wide email addresses available to his profile:

In addition, Acme Inc would prefer the system to choose the correct address to avoid unexpected errors.

So how does the system, automatically know which email address to set the from email?  With a little bit of Apex, and some practical Org Wide Address usage, we can achieve this requirement very quickly. 

The following code snippet is a Apex Controller Extension that leverages a custom field called domainname__c, on the Account object to match the correct Org Wide Address (assuming the profile allows sending of course). You can take the example much further, but the basic idea of adding some logic to automatically match the correct OWA makes this new feature even more powerful, especially for organizations where agents may deal with multiple different companies in an outsourced support center.

public class OWATest
private final Account acct;
public OWATest(ApexPages.StandardController controller)
public void SendEmail()
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

//simple example i know -- it doesn't check profile, and may have multiple matches
//but it just for demo purposes :)
for(OrgWideEmailAddress owa : [select id, Address from OrgWideEmailAddress])
String[] toAddresses = new String[] {''};
mail.setPlainTextBody('hello there');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

August 11, 2009

Leave your comments...

Automating the selection of Org Wide Addresses