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
AmbigaRamAmbigaRam 

How to write the test code for sending email

Hi,

 

 

I wrote the code which sends mail when the case is created.

 

Can anybody help  to write the code for testing?

 

Tha following is the code.

trigger mailNotification on Case ( after insert) {
    contact CaseContact;
    
    for(case Cases :trigger.new){
                    //Fetch the related case contact.
                CaseContact = [SELECT Email FROM Contact WHERE Id = :Cases.ContactId];
                if (CaseContact != null && CaseContact.Email != null) {
    Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage();
    
    CaseNotificationmail.setToAddresses(new List<String> { CaseContact.Email });
    CaseNotificationmail.setReplyTo('ambigaraman@gmail.com');
    CaseNotificationmail.setSenderDisplayName('Salesforce Support');
    CaseNotificationmail.setSubject('New Case Created : ' + Cases.Id);
    CaseNotificationmail.setPlainTextBody('Your case:<b> ' + Cases.Id +' has been created.'+'To view your case <a href=https://na1.salesforce.com/'+case.Id);
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { CaseNotificationmail });
 }  
}

}

 Anyhelp is appreciated

hitesh90hitesh90

Hi Ram,

 

Below is the Test class as per your Requirement.

 

Test Class:

@istest
public class TestmailNotification{
    Private Static testmethod void TestmailNotification(){
        Contact objCon = new contact();
        objCon.LastName = 'Test LastName';
        objCon.Email = 'test@test.com';
        insert objCon;    
        
        Case Objcase = new case();
        Objcase.Status = 'New';
        Objcase.ContactId = objCon.id;
        Objcase.Origin= 'Phone';
        insert Objcase;
    }
}

 

Important :
Hit Kudos if this provides you with useful information and if this is what you where looking for then please mark it as a solution for other benefits.

Thank You,
Hitesh Patel
SFDC Certified Developer & Administrator
My Blog:- http://mrjavascript.blogspot.in/

 

Bhawani SharmaBhawani Sharma
Create a contact record in your test class and then use that as Contact id in Case record.
Like
insert contact;
Case cObj = new Case(ContactId = contact.Id);
insert case;
KaityKaity

Hitesh/ Bhawani,

 

I have a very basic question and donot understand the concept.

When to instantiate and when not to instantiate??? please help.

 

trigger mailNotification on Case ( after insert) {
//    contact CaseContact; (have commented)
    contact CaseContact = new Contact();  (I have newly added)
    for(case Cases :trigger.new){
                    //Fetch the related case contact.
                CaseContact = [SELECT Email FROM Contact WHERE Id = :Cases.ContactId];
                if (CaseContact != null && CaseContact.Email != null) {
    Messaging.SingleEmailMessage CaseNotificationmail = new Messaging.SingleEmailMessage();
    
    CaseNotificationmail.setToAddresses(new List<String> { CaseContact.Email });
    CaseNotificationmail.setReplyTo('ambigaraman@gmail.com');
    CaseNotificationmail.setSenderDisplayName('Salesforce Support');
    CaseNotificationmail.setSubject('New Case Created : ' + Cases.Id);
    CaseNotificationmail.setPlainTextBody('Your case:<b> ' + Cases.Id +' has been created.'+'To view your case <a href=https://na1.salesforce.com/'+case.Id);
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] { CaseNotificationmail });
 }  
}

}