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
Achilles21Achilles21 

Trigger Test Class

Hi All,

 

I have a trigger ( After Update). I want to write a test class for it. The idea is to send an Email to the concerned person upon modification of a note. Here Note is a lookup with Account. And an Account will have a contact. The email address is of the contact.

 

Code is :

Hi All,

I am facing one of the worst test class scenarios. I have a trigger ( After Update). The code is:

Trigger sendEmail on Note__c (after update)
{

 User pu;
 List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>();
 
 pu= [select u.Contact.Account.Rep1__r.Name,
 
 u.Contact.Account.Rep2__r.Name,
 u.Contact.Account.Rep1__r.Email,
 u.Contact.Account.Rep2__r.Email from User u where  u.id=:UserInfo.getUserId()]; 
 
 
 String Smail= pu.Contact.Account.Rep1__r.Email;
 String Imail= pu.Contact.Account.Rep2__r.Email;

 
 system.debug('Toaddress:'+Smail);

 
 List<User> listOfUser = [Select u.Name From User u WHERE Id=:Userinfo.getUserId() limit1];
 
 List<Note__c> nt = new List<Note__c>();

 nt = [select Note1__c,Note2__c from Note__c where id IN :Trigger.New];
 
  
 for(Note__c n : nt)
 {
 
  Note__c oldNote = Trigger.oldMap.get(n.ID);
 
  snew = n.Note1__c;
  sold = oldNote.Note1__c;
  inew = n.Note2__c;
  iold = oldNote.Note2__c;
 }

 
 for(User us:listofUser)
 {
    username=us.Name;
 }

    if(sold<>snew)
	{
      Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
      String[] toAddresses = new String[] {Imail}; 
      mail.setSubject('Samsung Notes Modified');
      mail.setToAddresses(toAddresses);
    
      mail.setHTMLBody('The Email Body');
      Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
     
     }
     
     
     
     }
}

 

 

Can anyone please help me with the test class? I have researched enough but I am not getting any coverage!

 

gbu.varungbu.varun

Hi

You  can use @isTest(SeeAllData=True) in the tese case

AnushaAnusha

Insert Note__c object Reocrd in Test Class, update it.

Then Trigger will fire

Achilles21Achilles21

Yes Anusha!

 

The trigger fires. ... that's not the issue ...

 

System.DmlException: Update failed. First exception on row 0 with id a0lL0000000tCOcIAM; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, sendEmail: execution of AfterUpdate

caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Invalid to address : null: []

 

This is the error I am getting and I think it's cuz the Email address is not getting assigned to the variable. I guess the query is not fetching proper data.

AnushaAnusha

Yeah, Email Address is not picking up

gbu.varungbu.varun

pass the following values

mail.setTargetObjectId(Contact.id);

 

mail.setWhatid(Account.id);//Optional

 

Test in debug is there any value for

"String Imail= pu.Contact.Account.Rep2__r.Email;"

 

Achilles21Achilles21
@isTest(SeeAllData = True)

private class sendEmailTest
{
    static testMethod void sendEmailTest()
    {
    
   
     
          Note__C nt =new Note__c();
          nt.Name = 'Test Note';
          nt.Note1__c ='Comment#1';
          nt.Note2__c = 'Commment#2';
          insert nt;


         User pu= [select u.Contact.Account.Rep1__r.Name,
 
 u.Contact.Account.Rep2__r.Name,
 u.Contact.Account.Rep1__r.Email,
 u.Contact.Account.Rep2__r.Email from User u where  u.id=:UserInfo.getUserId()]; 
 
      
     String Smail= pu.Rep1__r.Email;
      system.debug('SMAIL+++++++++'+Smail);
     String Imail= pu.Rep2__r.Email;
      system.debug('IMAIL+++++++++'+Imail);
     String acc = pu.Contact.Account.Name;
      
      List<User> listOfUser = [Select u.Name From User u WHERE Id=:Userinfo.getUserId() limit 1];
      system.assertEquals(1,listofUser.size());
       
       Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        String[] toAddresses = new String[] {Imail}; 
        system.debug('IMAIL+++++++++'+toAddresses);
        mail.setToAddresses(toAddresses);
        mail.setHTMLBody('Hi');
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
        Messaging.SingleEmailMessage mail1 = new Messaging.SingleEmailMessage();
     
     Test.startTest();
          nt.Note1__c = 'Comment #2';
          nt.Note2__c = 'Comment#3';
          update nt;
          
     Test.stopTest();
      
    }
  


}

 This is my Test class

Achilles21Achilles21

One Weird thing I have come across...

 

Whenever I am updating a Note in the test class... instead of using the test class'  sendEmail method... the code is using External i.e. The original trigger's sendEmail method.

 

Thats why the trigger is failing.

 

any help?