I am working on writing a test class for an email trigger. The trigger fires when a new Community user self-registers and the Self_Registered__c formula field on Contact is checked. The trigger works fine when I test it, but I am struggling to get the test class.

Here's the trigger:

trigger LearnerWelcomeEmail on Contact (after insert) {

//Prepare to send welcome emails
List<Messaging.SingleEmailMessage> welcome = new List<Messaging.SingleEmailMessage>();

for (Contact learner : Trigger.New){
    if(trigger.isInsert && learner.Self_Registered__c == true)
       Messaging.SingleEmailMessage learnerEmail = new Messaging.SingleEmailMessage();
            List<String> sendTo = new List<String>();
            learnerEmail.setSenderDisplayName('Digital On-Ramps');



And here's the test class so far (it passes when I run a test, but provides no coverage.):

@isTest public class TestLearnerWelcome {
@isTest public static void emailUser(){
    Id p = [SELECT id FROM Profile WHERE name = 'Learner Profile Community User'].id;

    Account acct = new Account(name= 'TestAcct');
    insert acct;

    User u = new User(alias = 'learner1', email = 'learner@test.edu', emailencodingkey='UTF-8', lastname='Learner', languagelocalekey='en_US',
            localesidkey='en_US', profileid = p, country='United States',IsActive =true,
            timezonesidkey='America/Los_Angeles', username='tester@noemail.com');

    Site.createExternalUser(u, acct.Id);



I'm very new to writing code, so any help would be greatly appreciated.

  • What is the criteria for the formula field to be true? – Eric Nov 4 '15 at 16:32
  • A few "new coder" tips -- (1) the hard coded email template ID, if this is a new template not in PROD, will not have the same ID as in your sandbox. Best practice is to query for the template using the template's API name (that in turn comes from a custom setting or other configuration object). (2) You are limited on the number of apex emails you can send per 24 hours - see doc - depends on Edition - but limit is low. Better practice is to use workflow or process builder to send the emails to avoid the limit – cropredy Nov 4 '15 at 20:13
  • @Eric The Created By user must be the ePortfolio Site Guest User. Our Customer Community registration process doesn't check the IsSelfRegistered box. – Elizabeth Gibbons Nov 5 '15 at 18:23
  • @crop1645 Thanks for the tips! I'll make that change for the template. I tried workflow and process builder already and they cannot send an email to a just registered user. – Elizabeth Gibbons Nov 5 '15 at 18:26
  • The reason I asked is I 1. do not see you setting the contactID for the user you are creating and 2. I do not see you setting the Self_Registered__c = true. So you will have to meet those conditions either by creating records / code in your test to do it, or rely on existing codebase if that is what the current process is. – Eric Nov 5 '15 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.