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
jaishrijaishri 

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;
        
    }

 
Best Answer chosen by jaishri
Sai PraveenSai Praveen (Salesforce Developers) 
Hi Jai,

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

Sai PraveenSai Praveen (Salesforce Developers) 
Hi Jai,

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,
 
This was selected as the best answer
jaishrijaishri
Hi Sai, i have requirment of inserting 200 records in case using test class but data is not inserting there it is showing error like.......
@isTest
    public static void testmethod3(){
        List<Contact> conId = new List<Contact>();
        Contact con = new Contact();
        con.LastName = 'Test contact';
        con.Email = 'test@testcontact.com';
        insert con;
        List<Case> caseList = new List<Case>();
        for(Integer i=1; i<=200 ; i++){
            Case cs = new Case();
            cs.Status = 'New';
            cs.Origin = 'Phone';
            cs.ContactId =con.Id;
            caseList.add(cs);
        }
        
        Test.startTest();
        insert caseList;
        Test.stopTest();
        
    }

User-added image