I need to create an Org. Wide Email address for my Test Class, and I can't seem to find documentation for this. One forum led me to believe it wasn't possible, but it wasn't clear if I was understanding it correctly. Advice?

  • If you're going to reference external resources, please link to them. This site is not a guessing game. – Adrian Larson Jul 12 '18 at 18:48
  • Sorry if I confused you - My question is : How do I set up an Org. Wide Email in a Test Class; I cannot find documentation on it – Natalie Paige Jul 12 '18 at 18:52
  • You should be able to just query those records which exist. Regardless, please edit your post to either remove reference to other sites or actually specify which page you were looking at. – Adrian Larson Jul 12 '18 at 18:52
  • not without seeAllData, I thought? Using best practice, I'm creating the records for my rest class – Natalie Paige Jul 12 '18 at 18:53
  • See Isolation of Test Data from Organization Data in Unit Tests: ...objects that are used to manage your organization or metadata objects can still be accessed in your tests such as: User Profile Organization AsyncApexJob CronTrigger RecordType ApexClass ApexTrigger ApexComponent ApexPage... While OrgWideEmailAddress is not mentioned specifically, sure seems like setup data. – Adrian Larson Jul 12 '18 at 18:54

You can't set up an OrgWideEmailAddress. Attempting to do so gives this error:

DML operation Insert not allowed on OrgWideEmailAddress

Fortunately, you can see them perfectly fine without SeeAllData=true. I even took the time to verify it with a simple unit test:

// This class is in version 43.0, SeeAllData=false by default
@isTest class q224877 {
    @isTest static void test() {
        OrgWideEmailAddress[] addresses = [SELECT Id FROM OrgWideEmailAddress LIMIT 1];
        System.assertEquals(1, addresses.size());

This test, of course, passes. Typically speaking, when you're not sure if something applies to your scenario, write a unit test. Doing so saves you the trouble of trying to figure out something like this and waiting for an answer. This test took me about a minute to type up.

  • Hi, thank you for your answer - I knew SeeAllData would work, I've just been pressed time after time after time NOT to do that, so I was hoping the answer was in creating it with code; since that's best practice, I figured there was a way to create it that I missed and was just wondering what it was - I guess there isn't one. Sorry about that – Natalie Paige Jul 12 '18 at 19:26
  • @NataliePaige Do not use SeeAllData. There's no need, since it's a type of metadata that doesn't need this feature. – sfdcfox Jul 12 '18 at 19:27
  • @NataliePaige The best practice is to use the most appropriate method for the task at hand. For all "standard" objects, create your own data. For most "metadata" types, you can query them directly without creating them or using SeeAllData=true. – sfdcfox Jul 12 '18 at 19:28

To add on to @sfdcfox's answer it may be a good idea to setup a unit test to email a specifc Organziation-wide-Address to confirm 1.) it does exist 2.) It has been verified by the given email address. Maybe something like this?:

    OrgWideEmailAddress specificOrgWideEmailAddress = [SELECT Id, Address FROM OrgWideEmailAddress WHERE DisplayName = 'Display Name of Specific Org Wide address'];
    EmailTemplate emailTemplate = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'Test_Template'];

    List<Messaging.SingleEmailMessage> emailMessagesToSend = new List<Messaging.SingleEmailMessage>();

    Contact testContact = TestUtility.buildTestContact(<testcontactinfo>);
    insert testContact;
    Id contactId = testContact.Id;

    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();

        List<Messaging.SendEmailResult> results = Messaging.sendEmail(emailMessagesToSend ,false);

    Boolean emailsSentSuccessfully = results[0].isSuccess();
    System.assert(emailsSentSuccessfully , 'Email failure occurred. Confirm the Organization-Wide-Address is setup and VERIFIED for Specific Org Wide address given');

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.