You need to sign in to do that
Don't have an account?
Liam Lu 10
Write a Test Class for Trigger that Sends Emails
Hello
I wrote a trigger that sends an email order confirmation to the contact when an order object is inserted. However, I need to write a test calss for the code coverage. I am not sure how to do such a test. Any help will be apprecated. Thanks in advance.
Here is my trigger
trigger OrderConfirmatiob on Order (before insert) {
for(Order o : Trigger.New) {
if(o.Contact_Email__c != NULL){
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {o.Contact_Email__c};
mail.setToAddresses(toAddresses);
mail.setSubject('Automated email: Order Confirmation');
//mail.setTargetObjectId(o.Id);
//mail.setSaveAsActivity(false);
mail.setHtmlBody('Some Message' );
//mail.setTemplateId(et.Id);
emails.add(mail);
Messaging.sendEmail(emails);
//Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
Here is what I have so far for the test class but it is not finished.
@isTest
private class TestOrderConfirmationEmail {
public static void sendMail(string message) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'liam@shaverinc.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('My Subject');
mail.setUseSignature(false);
mail.setHtmlBody(message);
// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
static testMethod void TestOrderEmail(){
Account A = new Account(Name='ttt');
insert A;
Date myDate = Date.newInstance(2016, 2, 17);
//Create a order
Order od = new Order();
od.P_O_No__c = 'testponumber';
od.Order_Stage__c='Open';
od.Account.id=A.id;
od.EffectiveDate=myDate;
Test.startTest();
sendMail('This is a test');
insert od;
Test.stopTest();
}
}
Not sure how to test such a email trigger.
I wrote a trigger that sends an email order confirmation to the contact when an order object is inserted. However, I need to write a test calss for the code coverage. I am not sure how to do such a test. Any help will be apprecated. Thanks in advance.
Here is my trigger
trigger OrderConfirmatiob on Order (before insert) {
for(Order o : Trigger.New) {
if(o.Contact_Email__c != NULL){
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {o.Contact_Email__c};
mail.setToAddresses(toAddresses);
mail.setSubject('Automated email: Order Confirmation');
//mail.setTargetObjectId(o.Id);
//mail.setSaveAsActivity(false);
mail.setHtmlBody('Some Message' );
//mail.setTemplateId(et.Id);
emails.add(mail);
Messaging.sendEmail(emails);
//Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
}
Here is what I have so far for the test class but it is not finished.
@isTest
private class TestOrderConfirmationEmail {
public static void sendMail(string message) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
String[] toAddresses = new String[] {'liam@shaverinc.com'};
mail.setToAddresses(toAddresses);
mail.setSubject('My Subject');
mail.setUseSignature(false);
mail.setHtmlBody(message);
// Send the email
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
static testMethod void TestOrderEmail(){
Account A = new Account(Name='ttt');
insert A;
Date myDate = Date.newInstance(2016, 2, 17);
//Create a order
Order od = new Order();
od.P_O_No__c = 'testponumber';
od.Order_Stage__c='Open';
od.Account.id=A.id;
od.EffectiveDate=myDate;
Test.startTest();
sendMail('This is a test');
insert od;
Test.stopTest();
}
}
Not sure how to test such a email trigger.
Hope you are well!
I have excellent proficiency in Salesforce. I have gone through the code you have provided. I will assist you with exact resultion of your problem.
Please add me on Skype - seth.cis so that we can have a quick discussion.
Waiting for the reply.
Best Regards,
Seth Roger
Sktpe - seth.cis
Thanks for your quick reply. Yes, Contact_Email__c is a fomula feild. The fomula is just "Contact_Email__c=Quote.email" where quote email comes from the contact object that associated with each specific quote
In this case you need to create the Quote record in your test class and need to associate with order record in your test class.
I changed my test calss a lit bit. Here is the new test calss:
However, when I run the test , it will faid because for some reason the Quote ID is NULL. I am not sure why is that. How can I add this Quote object into my Order then? Thanks a lot!