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
Hitesh AlgamwarHitesh Algamwar 

test class for email class

 Can someone Let me know the Test class for  below code please :- 


public static void sendEmail(string emailId1 ,string otp1)
    {
        EmailTemplate emailTemplate = [select Id, Subject, HtmlValue, Body from EmailTemplate where name =:'AmazonOTP'];
        String plainBody = emailTemplate.Body;
        // integer ot = otp1;
        plainBody = plainBody.replace('{OTPData}', otp1);
        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.toAddresses = new String[] {emailId1 };
            message.optOutPolicy = 'FILTER';
        message.subject = emailTemplate.subject;
        message.plainTextBody = plainBody;
        Messaging.SingleEmailMessage[] messages =   new List<Messaging.SingleEmailMessage> {message};
            
            
            Messaging.SendEmailResult[] results = Messaging.sendEmail(messages);
        
        if (results[0].success) 
        {
            System.debug('The email was sent successfully.');
        } else 
        {
            System.debug('The email failed to send: ' + results[0].errors[0].message);
        }
        
    }
    
Best Answer chosen by Hitesh Algamwar
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the test class as below.
 
@Istest
public class SendEmailClassTest {
     static testmethod void triggerTest(){
         SendEmailClass.sendEmail('sample@gmail.com','sample');
     }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,

All Answers

Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

I guess the SOQL is having some issue I guess.
EmailTemplate emailTemplate = [select Id, Subject, HtmlValue, Body from EmailTemplate where name ='AmazonOTP'];

: is extra as AmazonOTP is string so no ened to add : 

Can you confirm if it is expecetd so I can share the test class for the same.

Thanks,
Hitesh AlgamwarHitesh Algamwar
Yes but that SOQL is fidning the template. NO worry you can just tell me the test class that will helpfull.

Thanks 
Sai PraveenSai Praveen (Salesforce Developers) 
Hi,

Can you try the test class as below.
 
@Istest
public class SendEmailClassTest {
     static testmethod void triggerTest(){
         SendEmailClass.sendEmail('sample@gmail.com','sample');
     }
}

Let me know if you face any issues.

If this solution helps, Please mark it as best answer.

Thanks,
This was selected as the best answer
Hitesh AlgamwarHitesh Algamwar
Yes thank you. Will let you know once checked.
Hitesh AlgamwarHitesh Algamwar
Yes Sai Praveen... Thanks for your Help.