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

How to Write a Test Class that covers the Single email Message in Trigger

Hi All,

trigger SendNotification on Savings_Risk_Checklist__c (After insert,after update) {

  for(Savings_Risk_Checklist__c SCS { 
 if (Trigger.isAfter && (Trigger.IsInsert || Trigger.IsUpdate)) { 

          string str = '';
               str=str+'<p>  Hello,<br/> </p>';
               str=str+'<p>  This notification is to inform you that a Sales Leader has just submitted a Savings Risk Checklist form. Please follow the link below to review the form. <br/> </p>';
               str=str+'<p>  '+
SCS.Savings_Risk_Checklist_Link__c+'  <br/> </p>';
               str=str+'<p>  If you have any issues accessing the link or form, please contact Melissa Umeda ( <br/> </p>';
               str=str+'<p>Thanks,<br/> </p>';
               str=str+'<p>GWS Salesforce Operations Team<br/> </p>';

   if(SCS.Savings_Checklist_Form_Status__c =='Proposal Submitted'|| SCS.Savings_Checklist_Form_Status__c=='Final From Submitted') {
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
         //EmailTemplate et=[Select id from EmailTemplate where Name=:'Savings Risk Check List Proposal Submittion'];
         mail.setToAddresses(new List<String>{''});
         Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});

Can Any one Suggests how can write a Test class for Above Trigger,

Raj VakatiRaj Vakati
Hi Satya , 
I believe the below code is enough to get the complete code coverage, Take below points into consideration.
  • Insert complete data  into the object saving risk check 
  • Follow the best practices while writing test classes 
  • Before writing this test class, i would say go ahead and look into the apex test classes trail head. 
  • Use system.asset in the test class 
private class TestClass {
    private static testmethod void testcasepositive(){
		// Insert Savings_Risk_Checklist__c 
		Savings_Risk_Checklist__c  sr = new Savings_Risk_Checklist__c () ; 
		sr.Savings_Checklist_Form_Status__c  ='Proposal Submitted' ;
		insert sr ; 
		sr.Savings_Checklist_Form_Status__c ='Final From Submitted' ;
		update sr ;

Hi Rajamohan,

Actually in the Above code 'Savings_Checklist_Form_Status__c' is a formula field in 'Savings_Risk_Checklist__c' from Opportunity.
i have master detail Relation ship to Opportunity. so when i tried to insert record with this field error giving 'Field Not writable'.

Could you please check the below test class,


private class SendNotificationTest{
public static testMethod void SendNotificationStaticTestMethod()
 Account a = NEW Account();
        a.Name = 'Test Account';
        insert a;

Opportunity o = NEW Opportunity();
        o.AccountId = a.Id;
        o.Name = 'Test Opportunity';
        o.Contract_Expiration_Date__c =system.Today()
        o.Savings_Checklist_Form_Status__c='Proposal Submitted';
        insert o;
        o.Savings_Checklist_Form_Status__c='Final Form Submitted';
        Update o;
   Savings_Risk_Checklist__c SCSList = new Savings_Risk_Checklist__c();
   Messaging.SingleEmailMessage Testemail;
   List<Messaging.SendEmailresult> Testemailresults;
   insert SCSList;
//      Below data covers if you trigger logic  for outgoing email 
//     That will be decided based on flag Incoming (default is false)
  EmailMessage outGoingMail= new EmailMessage();
         outGoingMail.toAddress = 'con1.Email';
         outGoingMail.subject = 'Opt Out Test Message';
         outGoingMail.TextBody= 'This is the message body BR-Interno.';;
         insert outGoingMail;          

It's Giving the 65% Code coverage.