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
Vasu.PVasu.P 

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 :trigger.new) { 
  
 
 if (Trigger.isAfter && (Trigger.IsInsert || Trigger.IsUpdate)) { 

          string str = '';
               str='<html>';
               str=str+'<body>';
               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 (melissa.umeda@cbre.com). <br/> </p>';
               str=str+'<p>Thanks,<br/> </p>';
               str=str+'<p>GWS Salesforce Operations Team<br/> </p>';
               str=str+'</body>';
               str=str+'</html>';

   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.setWhatId(SCS.ID);
         mail.setTargetObjectId(userinfo.getUserId());
         //mail.setTemplateId(et.id);
         mail.setTargetObjectId(SCS.CreatedById);
         mail.setTreatTargetObjectAsRecipient(false);
         mail.setToAddresses(new List<String>{'Test@gmail.com'});
         mail.setSaveAsActivity(false); 
         mail.setHtmlBody(str);
         Messaging.SendEmailResult [] r = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
     } 
 }
 }
}

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


Thanks,
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 
@isTest
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 ;
	
	}
}




 
Vasu.PVasu.P
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,

@Istest(SeeAllData=True)


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();
   SCSList.Opportunity_Name__c=o.ID;
   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.fromaddress='test@test.com';
         outGoingMail.toAddress = 'con1.Email';
         outGoingMail.subject = 'Opt Out Test Message';
         outGoingMail.TextBody= 'This is the message body BR-Interno.';
         outGoingMail.parentid=SCSList.id;
         insert outGoingMail;          
    
   
    
}
}


It's Giving the 65% Code coverage.

Thanks,