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
Liam Lu 10Liam 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. 
Amit Chaudhary 8Amit Chaudhary 8
IF "Contact_Email__c " field on order object is formula field ? if yes post your formula.
Seth RogerSeth Roger
Hi Liam Lu 10,

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 
Liam Lu 10Liam Lu 10
Hello Amit

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
Amit Chaudhary 8Amit Chaudhary 8
Hi Liam Lu 10,

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.
Liam Lu 10Liam Lu 10
Hi This is my new test class: static testMethod void TestOrderEmail(){ Account A = new Account(Name='ttt'); insert A; Quote Q=new Quote(); Q.Name='TestQuote'; Q.Status='Deaft'; Q.Payment_Terms__c='Payment Schedule'; Q.FOB__c='Our Shop'; Q.Freight__c='Collect'; Q.Lead_Time__c='TBA'; Q.Email='liam@shaverinc.com'; Q.ID='000001'; 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.Status='Draft'; od.AccountId=A.ID; od.Quote.id=Q.id; od.EffectiveDate=myDate; //Test.startTest(); sendMail('This is a test'); insert od; //Test.stopTest(); //System.assert('testponumner'==od.P_O_No__c); } 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! _____
Liam Lu 10Liam Lu 10
Sorry about the poor format. I will replay again here
I changed my test calss a lit bit. Here is the new test calss:
 
static testMethod void TestOrderEmail(){
        Account A = new Account(Name='ttt');
        insert A;
        
        
        Quote Q=new Quote();
        Q.Name='TestQuote';
        Q.Status='Deaft';
        Q.Payment_Terms__c='Payment Schedule';
        Q.FOB__c='Our Shop';
        Q.Freight__c='Collect';
        Q.Lead_Time__c='TBA';
        Q.Email='liam@shaverinc.com';

        

        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.Status='Draft';
        od.AccountId=A.ID;
        od.Quote.id=Q.id;
        od.EffectiveDate=myDate;
        
        sendMail('This is a test');
        insert od;
}


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!
Sachin Pawar 56Sachin Pawar 56
Hi Liam Lu 10, looks like you are not inserting the Quote Q before using the Q.id. After this statement Q.Email='liam@shaverinc.com';  Insert the Quote, insert Q;