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.


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.setToAddresses(new List<String>{'fake@email.com.fake'});
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


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

  • I got out of Salesforce development shortly after asking this question (that was the second-to-last day of that job, by my choice), so I can't test your answer, but it looks good so I'll accept it. – tomlogic Oct 2 '19 at 17:06

Unfortunately, from what I've read, what you want to do isn't possible.

According to the Objects API, you can find all the supported calls at this link which includes the schema call you've already made. That being said, despite what's implicit in the API, DML is not allowed on the OrgWideEmailAddress object.

On the IdeaExchange, you'll find the following submission: Allow DML for OrgWideEmailAddress, EmailServicesFunctio And EmailServicesAddress which also includes "Status of OrgWideEmailAddress needs to be added". Perhaps you might want to go to the link and add your vote.

  • Do we have a better solution to this now? After exploring for a while, realized there isn't be a better solution for this yet. – Anee Dec 6 '19 at 4:49
  • @Anee The only possible solution I can think of off hand would be to try to leverage AndrewFawcett's Apex-MDAPI from GitHub to retrieve the OrgWideEmail Addresses. It wouldn't help you much with unit testing unless you created a TestObjectFactory Class with it, but I'd expect it to allow you to retrieve them. The latter is primarily what the OP's question was about. – crmprogdev Dec 9 '19 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.