+ Start a Discussion
Natalya MurphyNatalya Murphy 

Testing note for OrgWideEmailAddress - how to ensure you can retrieve data during testing

Ran into this weirdness when I got inconsistent test results between 2 sandboxes:
-In one sandbox, a test failed because a query failed to retrieve records from OrgWideEmailAddress
-In another sandbox for the same org, the test passed and successfully retrieved the record from OrgWideEmailAddress

In both scenarios, the code being tested was looking for a specific email address which happened to match the logged-in user's real email.  For purposes of this post, let's say the user's email was myemail@myemail.com.   This same email was also listed as a record in OrgWideEmailAddress.   The code being tested specifically looked for this email address* (*I know, I know, bad practice to hard-code stuff like this, but I just inherited the system and was told to keep changes to a minimum).

Anyway, after checking to see that both sandboxes did ineed have an entry for myemail@myemail.com in OrgWideAddress, I digged a little deeper and discovered that:
- in the sandbox where the test succeeded, the logged-in user's email had been corrected from "myemail=myemail.com@example.com" to "myemail@myemail.com"
- in the sandbox where the test failed, the logged-in user's email had not been corrected and still showed Salesforce's sandboxified version of "myemail=myemail.com@example.com"

When I corrected the logged-in user's email in the failing sandbox to be "myemail@myemail.com", the previously-failed test succeeded.

So it appears that some behind-the-scenes magic happens when running tests that (maybe?) uses the logged-in user's email as the supposed OrgWideEmailAddress.

Hopefully this will help resolve a headache for someone else in the future.