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
Valentina Doda 7Valentina Doda 7 

Apex testing class

Hello everyone, 
I am working on a test class in apex and this is the class:
global class Flow_Dynamic_EmailAlerts {
    //@AuraEnabled public String emailTemplateName;
    //@AuraEnabled public List<User> recipients;    @InvocableMethod(label='Send request alert' description='Given a list of users it sends an email with email template to the users')
    public static void innerMethodInvocable(List<Flow_Dynamic_EmailAlerts_Request> request){
        EmailTemplate emailTemplate = [
            SELECT Id, Subject,Description, HtmlValue,DeveloperName,Body
            FROM EmailTemplate
            WHERE DeveloperName =: request[0].emailTemplateName
        ];        Set<String> userIdsSet = new Set<String>(request[0].recipients);
        List<User> users = [SELECT Id, Email FROM User WHERE Id IN:new List<String>(userIdsSet)];
        List<String> addresses = new List<String>();        for(User recipient : users) addresses.add(recipient.Email);        Messaging.SingleEmailMessage message = new Messaging.SingleEmailMessage();
        message.setTargetObjectId(UserInfo.getUserId());
        message.setSenderDisplayName('Salesforce Alert');
        //message.setReplyTo(recipient.Email);
        message.setUseSignature(false);
        message.setBccSender(false);
        message.setSaveAsActivity(false);        //Get templete id for set the templete.        message.setTemplateID(emailTemplate.Id);
        //message.setWhatId(account.Id); //merge fields ???
        message.toAddresses = addresses;        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);
    }    global class Flow_Dynamic_EmailAlerts_Request {
        @InvocableVariable(required=true)
        global String emailTemplateName;        @InvocableVariable(required=true)
        global List<String> recipients;
    }
}

I am trying to test the class but I cannot understand how to start. All the methotds I've try doesn't work. 

Can sb please see it and explain somehow how shoult I start?
Thanks very much

CharuDuttCharuDutt
Hii Valentina 
Try Below Test Class 100% Code Coverage And In Email Message  The Body Is Misssing  So Given The Eamplr To Add Body In Email Message
@IsTest
public class Flow_Dynamic_EmailAlertsTest {
	@isTest
    public Static void unitTest(){
        Flow_Dynamic_EmailAlerts.Flow_Dynamic_EmailAlerts_Request fdea = new Flow_Dynamic_EmailAlerts.Flow_Dynamic_EmailAlerts_Request();
        fdea.emailTemplateName = 'flight_update';
        fdea.recipients = new list<String>{'0052w000004hfXJAAY'};
        list<Flow_Dynamic_EmailAlerts.Flow_Dynamic_EmailAlerts_Request> lstfdea  = new list<Flow_Dynamic_EmailAlerts.Flow_Dynamic_EmailAlerts_Request>();
        lstfdea.add(fdea);    
        Flow_Dynamic_EmailAlerts.innerMethodInvocable(lstfdea);
    }
}

##################################################


Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
            mail.setSenderDisplayName('Email Alert');
            mail.setSubject('Account Update Info');
            String body = 'Your account information has been updated successfully.';
            mail.setToAddresses(new list<String>{con.Email});
            mail.setHtmlBody(body);
Please Mark It As Best Asnwer If It Helps
Thank You!