1

I have a trigger to send an email alert. I am trying to test the single email message portion of my trigger; however, I am receiving an error. Any help on testing the actual email portion of the trigger is much appreciated. Thank you in advance.

Need to test:::

if(prev.echosign_dev1__status__c != 'signed')
{                
    Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
    message.settargetobjectid(carriers.get(a.id).Insurance_email__c);
    String content = String.format('Testing the new agreement email.\n\n{1}/{2}\n\n',
    new string[] {
        system.url.getSalesforceBaseUrl().toexternalform(),
        a.id,
        a.name
    }
    );
       message.setPlainTextBody(content);
       message.setSubject('Agreement Signed ');
       message.setSaveAsActivity(false);
       messages.add(message);
}
if(!messages.isempty()) {
    Messaging.sendEmail(messages);
}

This is my test class that gets the error "ENTITY IS NOT ORG-ACCESSIBLE" at line 2

@isTest(seeAllData=true)
class TestSendEmailForInsuranceTriggertest {
        static testmethod void test(){
            carrier__c c = new carrier__c( name='test', insurance_email__c = 'test@test.com');
            insert c;

        echosign_dev1__SIGN_Agreement__c ag = new echosign_dev1__SIGN_Agreement__c(name='test', echosign_dev1__status__c = 'Signed');
        insert ag;

            if(ag.echosign_dev1__status__c == 'Signed'){   
                emailmessage em = new emailmessage();
                eml.fromaddress = 'Test@test.com';
                em.incoming = 'true';
                em.toaddress = 'test1@test.com';
                em.subject = 'Test Email';
                em.textbody = 'testing';
                em.parentid = c.id;
                test.starttest();
                insert em;
                test.stoptest();
            }
    }
    }
5

I think you are confused between the Messaging.sendEmail(messages) and the SObject EmailMessage used in the testclass.

  • EmailMessage is only used as a child of Case and is used to track incoming/outgoing emails on Cases
  • Messaging.sendEmail(..) will send an email but can't be tested using testmethods as testmethods won't send emails. The code will be 'invoked' but the email not sent (for obvious reasons - your testmethods could spam legitimate email addresses).

If you are concerned about whether you email message is being constructed correctly (hence verifying it with a testmethod), you'll want your prod code to break apart the construction of the email (perhaps by using a separate class) from the actual sending of the email. The testmethod can verify that the construction is accurate and you'll have to rely on faith that SFDC will actually send the email in PROD (which it will unless you've run out of daily apex emails -- something, BTW, you should reserve and test for in PROD code if the consequences of not sending the email are high. This is well-covered in the Apex Developer's Guide under outbound email.

UPDATE: With Enhanced Email, EmailMessage can be child of custom objects and most standard objects

1
  • Your sir are a scholar and a gentleman. I was able to code the class and trigger. Achieved 100% test coverage in the process. – Berg Mar 14 '14 at 15:54

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.