Automating the selection of Org Wide Addresses

Org Wide Address (OWA) is a great new feature recently added to Salesforce.com 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 support@acme.com. 

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 acme.com, a company who provides outsourced support centers for the following 3 companies: bigbrain.com, mygreatstartup.com, quintonwall.com. Whenever a customer sends an email to support@quintonwall.com (or one of the other fictitious companies) and is received by Fred, all of his emails (from address) must come from support@quinton.com. The seamless user experience is important to acme.com'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)
{
this.acct=(Account)controller.getRecord();
}
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])
{
if(owa.Address.contains(acct.domainname__c))
mail.setOrgWideEmailAddressId(owa.id);
}
String[] toAddresses = new String[] {'mycustomers@address.com'};
mail.setPlainTextBody('hello there');
mail.setToAddresses(toAddresses);
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}

Published
August 11, 2009
Topics:

Leave your comments...

Automating the selection of Org Wide Addresses