function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sparkysparky 

testing details of outbound email

I've got a Opp trigger which sends an email, and chooses one of a number of templates, based on details in the triggering Opportunity.

 

This works fine in general.  And I've written unit tests that make sure the code completes without error, and that the proper data gets written back to the Opp after the email goes out.

 

But I'd also like to test that the correct template gets used.  I don't see any way to test this directly, unless I write the template ID or some other detail to the database somwhere, because the unit test has no access to the Messaging object that contains the email details.  Am I understanding this correctly?

 

I know I could fake it by inspecting the Task records that get recorded.  But that's less than ideal, because the only details about the templates recorded in the Task are the subject lines and the email body, not the ID or template name.  In my case, the subject lines are identical for a few of the templates, and the email bodies are subject to constant change.  So neither is good for use in a permanent unit test.

 

So I'm faking it even worse by just using debug statements in the trigger to test this manually.  But I'd really like to be able to do it in a real unit test.

 

 Any bright ideas out there?

 

Thanks, y'all!

sfdccoder1sfdccoder1

Hello Sparky,
 
I would save the email templates record Ids in a cls file (as static variables) and in the unit test check that the correct template is used (this is similar to what you mentioned only here we keep the Ids in a cls file and not the DB).
I understand you think this solution is not good. Why?
I have this working pefectly in a numbe of organizations. 
 
 
paul-lmipaul-lmi

saving the id's to the class directly probably isn't the greatest way to do that.

 

do something like

 

 

emailtemplate[] tList = [select id from emailtemplate];

 


 

 

to get your list of template ID's from the database, and then use asserts before your email send logic to ensure the selection you expect is the one that's happening.  you won't be able to access the email specifically, but you will be able to ensure the correct template id is being passed to that logic.

sfdccoder1sfdccoder1

And how would you ensure the selection you expect is the one that's happening?

How would you idenify each email template? 

paul-lmipaul-lmi
how do you select the template now?
sparkysparky

Well, I'm not sure what coder was thinking, but my confusion with your suggestion, Paul, is that the trigger and the test class are two different bodies of code.  And the send happens in the trigger, while the assertions happen in the test class.  So I don't see how you could write an assertion about the details of the email object prior to sending it.

 

Coder, I will try your suggestion.  I didn't know that a test class could directly access a static variable stored in a class by a trigger or other class.  I guess I thought they were different transactions/threads, and that the test had to get all of its "state" from the database.  I guess that I made that limitation up, so I'll try your way.

 

Thanks!