You need to sign in to do that
Don't have an account?

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!
Hi
You can use @isTest(SeeAllData=True) in the tese case
Insert Note__c object Reocrd in Test Class, update it.
Then Trigger will fire
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.
Yeah, Email Address is not picking up
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;"
This is my Test class
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?