You need to sign in to do that
Don't have an account?
jaishri
hello , I want to write a test class to achieve 100 % code coverage for this below code can anyone help me to solve this problem i don't know how to write a test class for using this trigger
Trigger: trigger CaseEmail on Case (after insert, after update,before delete) { If((Trigger.isUpdate || Trigger.isinsert) && Trigger.isafter){//This line will allow only if the record is created or edited CaseEmailHandler.sendemailforInsrtupdate(Trigger.new); } if(Trigger.isDelete && Trigger.isBefore){// if the case is deleted we have to send an email so using this line and we used before because after deletion we cannot send email CaseEmailHandler.sendemailfordelete(Trigger.old); } } Handler: public class CaseEmailHandler { public static void sendemailforInsrtupdate(List<Case> caselist){ Set<Id> conIds = new Set<Id>(); List<Messaging.SingleEmailMessage> mails = new List<Messaging.SingleEmailMessage>(); for (Case c: caselist) { conIds.add(c.ContactId);//adding the contact id so we can get the contact email for the case } Map<Id, Contact> conMap = new Map<Id, Contact>([SELECT Id, Email FROM Contact WHERE Id In :conIds]);// quering the contact so we get the contact email for (Case c : caselist) { if (c.status == 'Closed') {// checking if the status is closed in case of create or edit scenerio Contact relatedCaseContact = conMap.get(c.ContactId);//getting the contact information related to particular case Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage(); CaseNotificationmail.setToAddresses(new List<String> { relatedCaseContact.Email });//adding to address CaseNotificationmail.setReplyTo('sample@salesforce.com');//adding reply to address CaseNotificationmail.setSenderDisplayName('Salesforce'); //adding display name CaseNotificationmail.setSubject(' Case Status updation ' + 'Changed to ' + c.status + ' Case Number:' + c.CaseNumber);//adding subject CaseNotificationmail.setPlainTextBody(' Your case Status for Case Number: ' + c.CaseNumber + ' Related Case Contact:' +c.ContactId +' has been closed '); //adding body of the email mails.add(CaseNotificationmail); //adding the notification to the list so all the emails can be sent once } if(Trigger.isinsert && Trigger.isafter){// as we need a notification when a case is craeted Contact relatedCaseContact = conMap.get(c.ContactId); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); mail.setToAddresses(new List<String> { relatedCaseContact.Email }); mail.setSubject('New Case Create: '+ c.CaseNumber); String body = 'Case is created. Thank you for contacting us'; mail.setHtmlBody(body); mails.add(mail); } } Messaging.sendEmail(mails);//sending the emails at once } public static void sendemailfordelete(List<Case> caselist){ Set<Id> conIds = new Set<Id>(); for (Case c: caselist) { conIds.add(c.ContactId); } List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>(); Map<Id, Contact> conMp = new Map<Id, Contact>([SELECT Id, Email FROM Contact WHERE Id In :conIds]); for (Case cs : caselist) { Contact relatedCaseContact = conMp.get(cs.ContactId); Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); email.setToAddresses(new List<String> { relatedCaseContact.Email }); email.setSubject('Case Deleted'); email.setPlainTextBody('This message is to alert you that the Case number' + cs.CaseNumber + ' has been deleted. Thank you for contacting us.'); emails.add(email); } Messaging.sendEmail(emails); } }
Just a small change in the test class. In the stack exchange and your question here you have after update event as well so the email send were 2 but in the above shared code there is no after update context so only one email will be send and it is while delete so changes it based on that.
If this solution helps mark it as best answer .
Thanks,
All Answers
I have answered the same question in stack exchange where you posted the same.
https://salesforce.stackexchange.com/questions/374496/how-to-write-a-test-class-for-this-below-code-can-anyone-help-me/374523#374523
Please check there and let me know if you face any issues.
if that solution helps, Please mark it as best answer.
Thanks,
Could you explain me this code line by line it will be more helpful for me
Please find the below explanation.
If this solution helps, please mark it as best answer.
Thanks,
From the test classes the emaila wont be send. It is just to check the code is working fine or not. The records created or updated wont be availble in salesforce org nor the emails sent as well. So we are checking using system.asserts. After the test class runs all the created records will be deleted.
It is the functionality of the test class.
If this answers your question please mark it as best answer.
Thans,
Thanks,
Did this answer your dobt or do you need any clarifiations.
Thanks,
the status for above code Assertion failed:- An email it should be sent Expected: 2,Actual: 4
how i understand the code is working properly and give me correct output
Can you check if there is any trigger on case sending the emails.
As per the code it should send only two emails(once updated and once deleted) but it is sending 4 emails so the asserion is failing.
Thanks,
For checking the test class you can also turn off other triggers so it won't impact this functionality and let me know if this works as expected.
I hope you are using the same test class which I shared in stack exchange.
Thanks,
Can you share your apex class and test class once so I can check it .
Thanks,
Just a small change in the test class. In the stack exchange and your question here you have after update event as well so the email send were 2 but in the above shared code there is no after update context so only one email will be send and it is while delete so changes it based on that.
If this solution helps mark it as best answer .
Thanks,
As confirmed earlier you cannot get any email or create records using test class. Test clases are like unit test scripts written to check the functionality.
Thanks,
@isTest static void setup() {
// Create common test accounts
List<Case> testCase = new List<Case>();
for(Integer i=0;i<1;i++) {
Case cs = new Case();
cs.Origin='Email';
cs.Subject='Test Case';
cs.Status = 'Closed case';
cs.To_Address__c= 'test@test.com';
testCase.add(cas);
}
insert testCase;
Delete testCase;
}
Can you share entire test class so I can check. This is just one method on the test class.
Thanks,
There is no contact information on the test class I did not get how will this send an email to the contact email address. If you are trying with another apex class and test class can you put it as new question as this thread is so long to verify any answer.
Thanks,
Yes I have shared as new question