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
NavneethNavneeth 

Test class for Send Email class

Hi,

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 = System.today();
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)
                    {
                         listofIDsToProcess.add(p.Id);
                          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){
          
           if(!String.isBlank(con.Email)){
               lcontactEmails.add(con.Email);
            }
        }
        }
        if(!lcontactEmails.isEmpty()){
            sendEmail(lcontactEmails); 
        }
    }

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

}
Abhi_TripathiAbhi_Tripathi
Hi Navneeth,

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

http://abhithetechknight.blogspot.in/2013/10/salesforce-test-class-basics.html

Regards,
Abhi Tripathi
Salesforce Certified Developer
Ashish_SFDCAshish_SFDC
Hi Navneeth, 


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

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

https://developer.salesforce.com/forums?id=906F00000009BTTIA2

http://stackoverflow.com/questions/10039223/apex-test-case-for-email-class

http://salesforce.stackexchange.com/questions/25838/test-class-for-sending-email-with-attachment


Regards,
Ashish