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 in a custom settings builder test utilityand 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. This code is shown below. The only other code in the test is the utility test classes that recreate our custom setting records that are necessary for the logic
I haven't included the main code because there's quite a lot going on andWhat I was thinking that it was relatedfound to somethingbe the issue, is in how we're forwarding the test since it passes withemail 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 46, but doesn't pass37 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 changingmessing with the API of just the test class. Otherwise, there's just a snippet that if instead of the from email matches a certain domain, it never savesAPI of the email message main code which is part of my confusion as well.
If I add setSaveAsActivity(or create a case/taskfalse) and justto the code snippet that forwards the email (sends an email) to the appropriate distributor. I assume this must be working since, the test passes and no case or task isemailMessages are created.
//create a new email and envelope object
Messaging.InboundEmail email = new Messaging.InboundEmail() ;
Messaging.InboundEnvelope envelope = new Messaging.InboundEnvelope();
List<String> ccAddresses = new List<String>();
ccAddresses.add('[email protected]');
ccAddresses.add('[email protected]');
// setup the data for the email
email.subject = 'Test email handler';
email.ccAddresses = ccAddresses;
email.plainTextBody = 'This is a test email body';
email.fromAddress = '[email protected]';
email.toAddresses = new string[]{'[email protected]', '[email protected]'};
envelope.fromAddress = '[email protected]';
envelope.toAddress = 'emailhandler@36alpdiwf1kcv419ekqhc9bmprxuzkzvqsg8xlwuxjakeq4ypf.e-ntkseae.el.apex.sandbox.salesforce.com';
//Add binary attachment
Messaging.InboundEmail.BinaryAttachment attachment = new Messaging.InboundEmail.BinaryAttachment();
attachment.body = blob.valueOf('my attachment text');
attachment.fileName = 'textfileone.txt';
attachment.mimeTypeSubType = 'text/plain';
email.binaryAttachments = new Messaging.Inboundemail.Binaryattachment[]{attachment};
//Add text attachment
Messaging.InboundEmail.TextAttachment attachmenttext = new Messaging.InboundEmail.TextAttachment();
attachmenttext.body = String.valueOf('my attachment text');
attachmenttext.fileName = 'textfiletwo.txt';
attachmenttext.mimeTypeSubType = 'texttwo/plain';
email.textAttachments = new Messaging.inboundEmail.TextAttachment[] { attachmenttext };
// call the email service class and test it with the data in the testMethod
UEH_UniversalEmailHandler testInbound = new UEH_UniversalEmailHandler();
testInbound.handleInboundEmail(email, envelope);
test.stopTest();
List<Case> createdCases = [SELECT Status,Origin,Subject,Description,Area__c,Sub_Area__c FROM CASE];
//Verify that no case has been created
System.assertEquals(0, createdCases.size());
//Verify that no EmailMessage record is created. This pulls 2 email messages when API is 37 in test class
List<EmailMessage> createdEmailMessage = [SELECT ParentId,Subject,FromAddress, toAddress, textBody FROM EmailMessage];
System.debug('email info ' + createdEmailMessage);
System.assertEquals(0, createdEmailMessage.size());
//Verify that no Task record has been created
List<Task> createdTask = [SELECT WhatId FROM Task];
System.assertEquals(0, createdTask.size());
Test Error on createdEmailMessage size assert. Pulls 2 email messages when there should be 0. Important to note the other 2 asserts pass in Salesforce.
System.AssertException: Assertion Failed: Expected 0, Actual: 2