You need to sign in to do that
Don't have an account?
Liz Gibbons 16
Test class for Email Trigger
Hello,
I am working on writing a test class for an email trigger. The trigger fires when a Community user self-registers. I'm struggling to get the test class to test this though.
Here's the trigger:
And here's the test class I have:
I currently have 28% coverage. Any insight would be greatly appreciated!
I am working on writing a test class for an email trigger. The trigger fires when a Community user self-registers. I'm struggling to get the test class to test this though.
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>(); sendTo.add(learner.Email); learnerEmail.setToAddresses(sendTo); learnerEmail.setReplyTo('info@digitalonramps.com'); learnerEmail.setSenderDisplayName('Digital On-Ramps'); learnerEmail.setTemplateId('00X17000000DnrL'); learnerEmail.setTargetObjectId(learner.Id); welcome.add(learnerEmail); } } if(welcome.size()>0){ Messaging.sendEmail(welcome); } }
And here's the test class I have:
@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; Contact con = new Contact(LastName = 'Learner', AccountId = acct.Id, email = 'learner@test.edu'); insert con; 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, ContactId = con.Id, timezonesidkey='America/Los_Angeles', username='tester@noemail.com'); insert u; System.runAs(u){ } } }
I currently have 28% coverage. Any insight would be greatly appreciated!
Please use the below test class : The If condition in your trigger is executing when "Self_Registered__c" field is set to true so in your test class you must set it as true.
Please use the above test class and let me know if you have any issue.
Thanks,
Abhishek
Using Site.createExternalUser() seems to have me on the right path (the class is passing), but I'm now at 0% coverage. Self_Registered__c is a formula field that looks at the Created By user. Here's what I have now:
Because Self_Registered__c is a formula field, I cannot set a value in the class. I am getting the above test class (using createExternalUser) to pass, but the code coverage for the trigger remains at 0%.
Please try with the below test class :
Let me know if you still have any issue with this class.
Thanks,
Abhishek
I just ran your class. It failed and got an error that I got yesterday trying a similar class.
Error Message: System.DmlException: Insert failed. First exception on row 0; first error: INVALID_CROSS_REFERENCE_KEY, Cannot create a portal user without contact: [ContactId]
Stack Trace: Class.TestLearnerWelcome.emailUser: line 15, column 1
Thanks,
Liz
You can give a try to the below test class :
I am not 100% sure that it will run successfully or not but you can give a try to this.
Let mer know if you any issue with it.
Thanks,
Abhishek
Same error as before. I think the issue lies in how our users are registering. I think I may need to talk with our development partners to figure this out.
Best,
Liz
Another thing you could try is querying the contact record in your trigger before you chek the Self Regsitered formula field like so:
I did try originally using IsSelfRegistered. Somehow our registration process is not ticking that box. So I'm left to create a custom field checked when the user is created by the 'eportfolio Site Guest User' which functions to give non-Community members access to certain of our pages.
The trigger itself is functioning correctly. Is there an advantage to doing as you do above? I'm a new developer, so I'm always looking to write better code than I currently am.