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

Test class for Send Email class


i need to write test class for the below mentioned class. This class sends Email to contacts of Accounts whose installed product is due for maintainence in the near future.

Pls help me write test class for this.

global class SendMail {
public List<Preventive_Maintenance_Coverage__c> installprods;
List<String> lcontactEmails = new List<String>();
List<Contact> con = new List<Contact>();
Set<ID> listofIDsToProcess = new Set<ID>();
public Date t =;
public Date criteria;

        public SendMail(){
                           installprods = this.Findinstallprods();
                          // installprods.clear();

public List<Preventive_Maintenance_Coverage__c> Findinstallprods() {
   // query to find only the cases that match your conditions requiring email alerts to be sent

   installprods = [select id, Installed_Product__r.Name,Installed_Product__r.Date_Installed__c,frequency__c from Preventive_Maintenance_Coverage__c];

    for(Preventive_Maintenance_Coverage__c p : installprods)
                Integer frequencyval = Integer.valueOf(String.valueOf(Math.roundToLong(P.frequency__c )));
                if(p.Installed_Product__r.Date_Installed__c + frequencyval - 5 == t)
                          system.debug('ID TO PROCESS' + listofIDsToProcess);
   return installprods;
   List<Account> accList  = [SELECT id,(SELECT Email FROM Contacts) FROM Account WHERE id IN (SELECT Account__c FROM Installed_Product__c WHERE Id IN: listofIDsToProcess)];
            if(accList.size() > 0) {
        for(Contact con : accList[0].contacts){

public static void sendEmail (List<String> toAddress){
        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        //set the email properties
        mail.setSenderDisplayName(' Email Agent');
        mail.setSubject('A new reminder');
        mail.setHtmlBody('Service Maintaince Reminder .');
        //send the email
        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail } );

Hi Navneeth,

You can start writing test class, by going through this post, hope this helps

Abhi Tripathi
Salesforce Certified Developer
Hi Navneeth, 

A static method is call always with class name. Call the SendEmail method with class name like SendConfirmationEmail. SendEmail(,; Instead of calling the with instance of class.

See the links below for more information and sample test code,