0

I am getting this error message:

Message: SendEmail failed. First exception on row 0; first error: UNKNOWN_EXCEPTION, java.lang.NullPointerException: []
Stack Trace:
Class.MessagingHelper.send: line 4, column 1
Class.RESTController.sendEmail: line 66, column 1
Class.RESTController.handlePost: line 41, column 1
Line Number: 4
Type Name: System.EmailException

When I run this code:

private static void sendEmail(Blob attachment, Id recordId) {

    Id targetObjectId = getContact();

    if (targetObjectId == null){
        throw new CustomException('Unable to find contact');
    }

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    mail.setTemplateId(getEmailTemplateId());    
    mail.setWhatId(recordId);
    mail.setTargetObjectId(targetObjectId);
    mail.setOrgWideEmailAddressId(OrgWideEmailAddressHelper.getNoReplyOrgWideEmailAddressId());             
    mail.setSaveAsActivity(true);
    mail.setOptOutPolicy('FILTER');
    mail.setFileAttachments(MessagingHelper.createEmailFileAttachment(FILE_TITLE, attachment));

    MessagingHelper.send(mail);
}

Which uses these helper methods:

public without sharing class MessagingHelper {

    public static void send(Messaging.SingleEmailMessage mail) {
        MessagingHelper.handleSendEmailResult(Messaging.sendEmail(MessagingHelper.createEmailMessages(mail)));
    }

    public static List<Messaging.EmailFileAttachment> createEmailFileAttachment(String filename, Blob body) {
        Messaging.EmailFileAttachment emailAttachment = new Messaging.EmailFileAttachment();
        emailAttachment.setFileName(filename);
        emailAttachment.setBody(body);
        return new List<Messaging.EmailFileAttachment> {emailAttachment};
    }

    public static List<Messaging.SingleEmailMessage> createEmailMessages(Messaging.SingleEmailMessage mail) {
        List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();
        messages.add(mail);
        return messages;
    }

    public static void handleSendEmailResult(List<Messaging.SendEmailResult> results) {
        for(Messaging.SendEmailResult result :results) {                                    
            if(!result.IsSuccess()) {                    
                List<Messaging.SendEmailError> errors = result.getErrors();
                if (errors.size() > 0){
                    Messaging.SendEmailError err = errors[0];
                    System.debug(err.getMessage());       
                }                   
            }
        } 
    }
}

What is causing it and how do I fix it?

  • 1
    You don't need to call the class' name to call its own methods. Odd line to throw that error...Messaging or MessagingHelper would have to be null. – Adrian Larson Jan 29 '19 at 23:21
2

The error was caused by incorrect permissions.

The RESTController is accessed via a public Site and the Site Profile needed access to the Email Template relatedTo object so that it could correctly merge the fields.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.