Any resources on what might've changed in API 37 that wasn't present in 36 that could be causing the issue I'm seeing?

If I update every other apex class in my org to 46 and leave the test class at 36, the test passes. If I bump the test class to 37, the test class fails.

We're essentially testing our email services apex class that checks for certain domains of where the email is coming from against a distributor email list (which is in a custom setting) and then forwards that email to the distributor and doesn't save the email in SF and doesn't create a case.

At the end of test class we query cases, emails, and tasks to confirm there's 0 records as the passed in email and envelope to the apex class should match a distributor email we created and we don't save the email message (just forward it along).

The test class, however finds email message records (but finds no case or task) which fails the assert.

What I found to be the issue, is in how we're forwarding the email to the distributor in the main code. We're not setting .setSaveAsActivity at all (so the default is true) and it seems like it now saves as an emailMessage record in API 37 and beyond whereas it did not in 36. I'm just trying to figure out if there's any documentation noting this? This only applies when messing with the API of the test class instead of the API of the main code which is part of my confusion as well.

If I add setSaveAsActivity(false) to the code snippet that forwards the email to the distributor, the test passes and no emailMessages are created in Salesforce.

  • You have three separate instances of system.assertEquals(0, ...), none of which include a message. Given that, your assertion failure message on its own is not sufficient to determine which line causes the failure. Please edit your post to be more explicit.
    – Adrian Larson
    Commented Oct 21, 2019 at 12:58
  • 1
    ALso you have not added the main class code Commented Oct 21, 2019 at 14:20
  • It'd be very difficult to add the main class code. I'll add more debugging and see if I can isolate to include some, I had assumed it was related to the test class as changing only that API (of the apex test) affects whether it passes or fails. Commented Oct 21, 2019 at 14:46
  • 1
    Try selective reduction. Eliminate mocking the attachments and try again. Keep reducing until you have something working, then add back.
    – cropredy
    Commented Oct 21, 2019 at 15:03
  • Thanks. I ended up looking deeper into what email records existed in the query and realized they were the forwarding email instead of the initial test email like I previously assumed. I've changed the question to see if I can pinpoint any documentation supporting what I noted with setSaveAsActivity() functionality and why it only struggles with the test apex being upgraded. Commented Oct 21, 2019 at 15:35

2 Answers 2


It seems Enhanced Email was introduced in API 37 which is what's causing the issue. Before that, the emails we forwarded in the code did not create an emailMessage record. Now, they do as the set setSaveAsActivity defaults to true. I can either use that method and set to false to not create an EmailMessage record or update my SOQL query to specifically search for email2case EmailMessages.

I assume test apex API takes precedence over the main apex API which is why only changing the test class affected it.


Just a shot in the dark, but I think it has something to do with Enhanced Email and it being enabled in version 37 Invalid type: Schema.EmailMessageRelation

  • 1
    I appreciate the attempt. I did narrow it down to the setSaveAsActivity method needing to be false when the test class API is greater than 36.0. I'm just not finding any info on this change or why it only becomes an issue when upgrading the test class versus upgrading the main code. Commented Oct 21, 2019 at 15:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .