You need to sign in to do that
Don't have an account?
AmbigaRam
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
Hi Ram,
Below is the Test class as per your Requirement.
Test Class:
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/
Like
insert contact;
Case cObj = new Case(ContactId = contact.Id);
insert case;
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 });
}
}
}