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('[email protected]'); //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);
}
}