You need to sign in to do that
Don't have an account?
jaishri
Hi , Can anyone explain me it is right way to send an email to contact case using this test class I'm confused .....
Trigger trigger CaseTrigger on Case (after insert,after delete) { system.debug('trigger value--> '+Trigger.isDelete+'and--> '+Trigger.IsAfter); CaseTriggerHandler CaseHandler = new CaseTriggerHandler(); if(Trigger.isInsert&&Trigger.IsAfter){ system.debug('inside insert trigger'); CaseHandler.AfterInsert(Trigger.new); } if(Trigger.isDelete&&Trigger.IsAfter){ system.debug('inside isDelete trigger'); CaseHandler.AfterDelete(Trigger.old); }
Handler class public class CaseTriggerHandler { public void AfterInsert(List<Case> case1){ system.debug('inside CaseTriggerHandler insert trigger '+case1); List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>(); TestEmailCase__mdt testEmail = [select id, Label,NamespacePrefix,To_Address__c from TestEmailCase__mdt]; system.debug('test case list -->'+testEmail); for(Case ca:case1){ system.debug('test email start -->'+testEmail.To_Address__c); Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); List<String> eid = new List<String>(); eid.add(testEmail.To_Address__c); email.setToAddresses(eid); email.setSubject('case create'); email.setPlainTextBody('case created'); emails.add(email); system.debug('test email end -->'); } Messaging.sendEmail(emails); } public void AfterDelete(List<Case> case1){ List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>(); TestEmailCase__mdt testEmail = [select id, Label,NamespacePrefix,To_Address__c from TestEmailCase__mdt]; system.debug(testEmail); for(Case ca:case1){ Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage(); List<String> eid = new List<String>(); eid.add(testEmail.To_Address__c); email.setToAddresses(eid); email.setSubject('case deleted'); email.setPlainTextBody('case deleted'); emails.add(email); } Messaging.sendEmail(emails); } }
Test Class @isTest public class CaseTriggerHandler_Test_class{ @isTest static void setup() { // Create common test accounts List<Case> testCase = new List<Case>(); for(Integer i=0;i<1;i++) { Case cas = new Case(); cas.Origin='Email'; cas.Subject='Test Case'; cas.Status = 'Closed case'; cas.To_Address__c = 'sample@sample.com'; testCase.add(cas); } insert testCase; Delete testCase; }
Firstly the code which you wrote send the email to single email mentioned in the custom metadata irrespective of the contact email. Is this the functionality you are looking for.
And coming to the test class question No even with this test class the email wont be send to the address. This is how the test class behaves. You can check if emails were invoacted or not as below.
https://apexandbeyond.wordpress.com/2017/07/30/unit-testing-code-that-sends-email/
If this solution helps, Please mark it as best answer.
Thanks,
All Answers
Firstly the code which you wrote send the email to single email mentioned in the custom metadata irrespective of the contact email. Is this the functionality you are looking for.
And coming to the test class question No even with this test class the email wont be send to the address. This is how the test class behaves. You can check if emails were invoacted or not as below.
https://apexandbeyond.wordpress.com/2017/07/30/unit-testing-code-that-sends-email/
If this solution helps, Please mark it as best answer.
Thanks,