1

Here is the code-

public static void sendEmailToUser(String email, String emailTemplateNameText, String selectedRecordId){
    
    String emailTemplateName = '%' + emailTemplateNameText + '%';
    String emailForQuery = '%' + email + '%';
    
    system.debug('selectedRecordId: '+selectedRecordId);
    EmailTemplate emailTemplate = [SELECT Name, DeveloperName, Id, Body 
                                   FROM EmailTemplate 
                                   WHERE Name like :emailTemplateName
                                   AND IsActive = true
                                   LIMIT 1];
    Contact dummyContact = [SELECT Id, Name, Email 
                            FROM Contact
                            WHERE Name <> null
                            AND Email <> null 
                            limit 1];
    User userRec = [SELECT Id, name, Username, email
                    FROM User
                    where Email like :emailForQuery
                    limit 1];
    OrgWideEmailAddress owa = [SELECT id, DisplayName, Address 
                               FROM OrgWideEmailAddress 
                               limit 1];
    
    System.debug('EmailTemplate: '+emailTemplate.Id);
    System.debug('email: '+email);
    System.debug('dummyContact.Id: '+dummyContact.Id);
    System.debug('selectedRecordId: '+selectedRecordId);
    
    String templateId   = emailTemplate.Id;
    String userId       = userRec.Id;
    String whatId       = selectedRecordId;
    
    Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
    email.setTemplateId(emailTemplate.id);
    email.setTargetObjectId(dummyContact.Id);
    email.setWhatId(selectedRecordId);
    email.saveAsActivity = false;
    List<string> toAddress = new List<string>();
    toAddress.add(email);
    email.setToAddresses(toAddress);
    
    if(!Test.isRunningTest()) { 
        try {
            Messaging.SendEmailResult[] sendEmailResult = Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
            
            for(Messaging.SendEmailResult r : sendEmailResult){
                if (r.success) {
                    System.debug('Successfully sent email to '+toAddress[0]);
                }else{
                    System.debug('The email failed to send: ' + r.errors[0].message);
                }
            }
        } catch (Exception e) {
            System.debug(e.getMessage());
        }
    }
}

The debug logs for this method reflect that the System debugs all have the correct values. The merge fields are also populating correctly, and the debug log at the success also executes. However the email isn't sent and I get the above exception. I'm using a custom email template for this example, if that might be an issue.

4
  • The problem doesn't appear to be here, especially if the final debug statement is being put out. However, note that you are sending two emails, not just one (setTargetObjectId is the recipient by default). Whatever index out of bounds error you're getting is in the code calling this code.
    – sfdcfox
    Commented Sep 14, 2022 at 13:08
  • @sfdcfox thanks, this method is called from the lightning component. Is there any way I can send just the one email I need to send?
    – Prapti
    Commented Sep 14, 2022 at 13:52
  • There's no method for sending an email through LWC, you do need Apex. Perhaps you're sending in bad data? Do you know which line of code is throwing the error?
    – sfdcfox
    Commented Sep 14, 2022 at 14:19
  • Hey @sfdcfox, there was an uncommented line System.debug('The email failed to send: ' + r.errors[0].message); in the success if statement, which was causing the issue. Now although I'm not getting the exception, the email is still not getting sent. I checked the Deliverability setting too, the access level is All email. Should I check any other settings that might be causing this?
    – Prapti
    Commented Sep 14, 2022 at 14:39

0

You must log in to answer this question.

Browse other questions tagged .