Skip to main content
edited tags
Link
Kris Goncalves
  • 22.8k
  • 8
  • 35
  • 72
Notice removed Draw attention by CommunityBot
Bounty Ended with no winning answer by CommunityBot
Found core issue, refined question to look for resources supporting my fix/work
Source Link
Kris Goncalves
  • 22.8k
  • 8
  • 35
  • 72

Test class failure only when updating test class past 36/API Difference with Regards to EmailMessage and Messaging.SendEmail()

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

Test class failure only when updating test class past 36

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 utility.

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 and I was thinking that it was related to something in the test since it passes with the main code at API 46, but doesn't pass when changing the API of just the test class. Otherwise, there's just a snippet that if the from email matches a certain domain, it never saves the email message (or create a case/task) and just forwards the email (sends an email) to the appropriate distributor. I assume this must be working since no case or task is 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.

System.AssertException: Assertion Failed: Expected 0, Actual: 2

Test/API Difference with Regards to EmailMessage and Messaging.SendEmail()

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.

added 600 characters in body; added 42 characters in body; added 1 character in body; added 20 characters in body
Source Link
Kris Goncalves
  • 22.8k
  • 8
  • 35
  • 72

We're essentially testing our email services apex class that will 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.

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 and I was thinking that it was related to something in the test since it passes with the main code at API 46, but doesn't pass when changing the API of just the test class. Otherwise, there's just a snippet that if the from email matches a certain domain, it never saves the email message (or create a case/task) and just forwards the email (sends an email) to the appropriate distributor. I assume this must be working since no case or task is created.

Test Error on createdEmailMessage size assert. Pulls 2 email messages when there should be 0. Important to note the other 2 asserts pass.

We're essentially testing our email services apex class that will 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.

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 is the utility test classes that recreate our custom setting records that are necessary for the logic.

Test Error

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.

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 and I was thinking that it was related to something in the test since it passes with the main code at API 46, but doesn't pass when changing the API of just the test class. Otherwise, there's just a snippet that if the from email matches a certain domain, it never saves the email message (or create a case/task) and just forwards the email (sends an email) to the appropriate distributor. I assume this must be working since no case or task is created.

Test Error on createdEmailMessage size assert. Pulls 2 email messages when there should be 0. Important to note the other 2 asserts pass.

Notice added Draw attention by Kris Goncalves
Bounty Started worth 100 reputation by Kris Goncalves
Source Link
Kris Goncalves
  • 22.8k
  • 8
  • 35
  • 72
Loading