Identify which OrgWideEmailAddress records are valid for current user

I’d like to create a popup list on one of my Visualforce pages, for a user to select from a list of OrgWideEmailAddress records. My goal is to limit the list to only those records that they’re allowed to use. I’m going to save the selection for use in later automatic emails.

Based on what I’ve read, I won’t be able to send emails to certain OrgWideEmailAddresses:

  • The address hasn’t been verified yet. I don’t see any fields in the schema that appear to show verification state.
  • The address is only available to profiles that the user isn’t a member of. I don’t see where the profile list is stored for each record.

Some of my problem could just be due to not understanding the standard objects so well, and perhaps there’s a way to get at this through UserInfo, or by making my query on the User object which may have a child relationship to OrgWideEmailAddress since it’s listed in the schema viewer of the Force.com IDE.

Here’s the test code I’m working with:

for (OrgWideEmailAddress addr : [SELECT DisplayName, Address 
        FROM OrgWideEmailAddress ORDER BY DisplayName]) {
    System.debug( addr.DisplayName + ' <' + addr.Address + '>');
}

I tried creating a new Messaging.SingleEmailMessage and then calling setOrgWideEmailAddressId() on it with each OrgWideEmailAddress Id, in hopes that invalid Ids would throw an exception, but to no avail.

My goal is to modify the test code to provide just the subset of records that the current user will be able to use.

Answer

There isn’t a standard way to test for this, but you can fake send an email and see what exceptions are thrown to see whether the running user has access to that org wide email address.

Boolean isAccessible = false;

Id oweaId = .....; // set your org wide email address id here

Messaging.SingleEmailMessage fakeMessage = new Messaging.SingleEmailMessage();
fakeMessage.setOrgWideEmailAddressId(oweaId);
fakeMessage.setToAddresses(new List<String>{'fake@email.com.fake'});
fakeMessage.setPlainTextBody('body');
fakeMessage.setEntityAttachments(new List<String>{SObjectType.ContentVersion.keyPrefix + '000000000123'}); // causes the email sending to fail
try {
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { fakeMessage });
} catch(emailException eEx) {
    isAccessible = (eEx.getDmlType(0) != StatusCode.INSUFFICIENT_ACCESS_OR_READONLY && eEx.getDmlType(0) != StatusCode.UNVERIFIED_SENDER_ADDRESS);
}

// Use 'isAccessible' variable as required

This never sends an email because the attachment doesn’t exist, so we’re checking the exception to see what’s stopping the email

Performance

The fake sending of the email took about a tenth of a second in my org, so if you’d want to be careful in using this.

Batching up multiple requests can reduce the per OWEA time.

Email Limits

This doesn’t use up your send email limits. You can test by adding Limits.getEmailInvocations() before and after this code

Attribution
Source : Link , Question Author : tomlogic , Answer Author : Nick Cook

Leave a Comment