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
Shruthi GM 4Shruthi GM 4 

Send email test class

/* class */

Global class emailHelper {
public static void sendEmail(ID recipient, ID candidate) {
//New instance of a single email message
Messaging.SingleEmailMessage mail =

            new Messaging.SingleEmailMessage();

  

// Who you are sending the email to

   mail.setTargetObjectId(recipient);

 

   // The email template ID used for the email

   mail.setTemplateId('');

           

   mail.setWhatId(candidate);   

   mail.setBccSender(false);

   mail.setUseSignature(false);

   mail.setReplyTo('recruiting@acme.com');

   mail.setSenderDisplayName('HR Recruiting');

   mail.setSaveAsActivity(false); 

 Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

 

    } 

}

/* testclass */


@istest

public class emailHelpertest{

public static testmethod void testvalidate(){

Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
Profile p = [SELECT Id FROM Profile WHERE Name='System Administrator']; 
User u = new User(Alias = 'test1', Email='test@gamil.com',FirstName = 'Test', 
            EmailEncodingKey='UTF-8', LastName='Testlast', LanguageLocaleKey='en_US', 
            LocaleSidKey='en_US', ProfileId = p.Id, 
            TimeZoneSidKey='America/Los_Angeles', UserName='test111@gmail.com');
            insert u;
ID candidate;
ID recipient;

Contact con=new Contact();
con.lastname='Testing';
insert con;

test.starttest();
//mail.setTemplateId('');
mail.setTargetObjectId(recipient);
mail.setWhatId(candidate);   
mail.setBccSender(false);
mail.setUseSignature(false);
mail.setReplyTo('recruiting@acme.com');
mail.setSenderDisplayName('HR Recruiting');
mail.setSaveAsActivity(false); 
emailHelper ehelp=new emailHelper();
emailHelper.sendEmail(recipient,candidate);
test.stoptest();
}
}

But this test class has an error saying invalid id.It is able to cover only 41%.Please help.
Best Answer chosen by Shruthi GM 4
TintuBabuTintuBabu
Hi,

Error occurs with templateId null, either specify an existing email template id or use  mail.setPlainTextBody('') method  and also specify email address for contact like this 


Global class emailHelper {
    public static void sendEmail(ID recipient, ID candidate) {
        //New instance of a single email message
        Messaging.SingleEmailMessage mail =
       new Messaging.SingleEmailMessage();
    // Who you are sending the email to
       mail.setTargetObjectId(recipient);
       // The email template ID used for the email
       //mail.setTemplateId('');
       mail.setPlainTextBody('');

       mail.setWhatId(candidate);   
       mail.setBccSender(false);
       mail.setUseSignature(false);
       mail.setReplyTo('tintu@qburst.com');
       mail.setSenderDisplayName('HR Recruiting');
       mail.setSaveAsActivity(false); 
       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

        } 

}


@istest

public class emailHelpertest{

    public static testmethod void testvalidate(){
    
        Account newAcc = new Account();
        newAcc.name='test';
        insert newAcc;
        
        Contact con=new Contact();
        con.lastname='Testing';
        con.email='test@test.com';
        insert con;
        
        
        emailHelper.sendEmail(con.Id,newAcc.Id );
    }
}


 

All Answers

TintuBabuTintuBabu
Hi ,

You can cover that class by calling
 
@istest

public class emailHelpertest{

public static testmethod void testvalidate(){

Account newAcc = new Account();
newAcc.name='test';
insert newAcc;

Contact con=new Contact();
con.lastname='Testing';
insert con;


emailHelper.sendEmail(con.Id,newAcc.Id );
}
}


Target id in your case reciepient must be a contact id. 

setTargetObjectId(targetObjectId)
Required if using a template, optional otherwise. The ID of the contact, lead, or user to which the email will be sent. The ID you specify sets the context and ensures that merge fields in the template contain the correct data.

What id in you case candidate can be of the following types:
Account
Asset
Campaign
Case
Contract
Opportunity
Order
Product
Solution
Custom

Hope this helps.
Shruthi GM 4Shruthi GM 4
Hi,
Even after changing the id to con.id,I am facing the same issue!!!!!
TintuBabuTintuBabu
Hi,

Can you please check in log in which line error occurs?
Shruthi GM 4Shruthi GM 4
Hi,here is the log.
User-added image
TintuBabuTintuBabu
Hi,

Error occurs with templateId null, either specify an existing email template id or use  mail.setPlainTextBody('') method  and also specify email address for contact like this 


Global class emailHelper {
    public static void sendEmail(ID recipient, ID candidate) {
        //New instance of a single email message
        Messaging.SingleEmailMessage mail =
       new Messaging.SingleEmailMessage();
    // Who you are sending the email to
       mail.setTargetObjectId(recipient);
       // The email template ID used for the email
       //mail.setTemplateId('');
       mail.setPlainTextBody('');

       mail.setWhatId(candidate);   
       mail.setBccSender(false);
       mail.setUseSignature(false);
       mail.setReplyTo('tintu@qburst.com');
       mail.setSenderDisplayName('HR Recruiting');
       mail.setSaveAsActivity(false); 
       Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

        } 

}


@istest

public class emailHelpertest{

    public static testmethod void testvalidate(){
    
        Account newAcc = new Account();
        newAcc.name='test';
        insert newAcc;
        
        Contact con=new Contact();
        con.lastname='Testing';
        con.email='test@test.com';
        insert con;
        
        
        emailHelper.sendEmail(con.Id,newAcc.Id );
    }
}


 
This was selected as the best answer
Shruthi GM 4Shruthi GM 4
Thanks a lot for your help...it worked..:)