0

I've written a method which should send messages to the current user, the System Administrator, and a list of other addresses that I specify.

public static void sendErrorEmail(String body)
{
        Messaging.SingleEmailMessage errorMail = new Messaging.SingleEmailMessage();
        errorMail.setSubject('Error E-mail Subjectline');
        errorMail.setPlainTextBody(body);
        String[] sendTo = new String[]{};
        sendTo.add(UserInfo.getUserEmail());
        Profile adminProfile = [Select Id, Name from Profile where Name = 'System Administrator' LIMIT 1];
        if(UserInfo.getProfileId() != adminProfile.Id) 
            sendTo.add([Select Email, ProfileId from User where ProfileId = :adminProfile.Id LIMIT 1].Email);

        errorMail.setToAddresses(sendTo);

        //errorMail.setCcAddresses(devEmails);
        errorMail.setTargetObjectId(UserInfo.getUserId());
        System.debug(errorMail.getTargetObjectId());
        System.debug(sendTo);


        Messaging.SendEmailResult[] eResults = Messaging.sendEmail(new Messaging.SingleEmailMessage[]{errorMail}, false);
        System.debug(eResults);
}

However, this is giving me the following error (copied from debug log):

DEBUG|(Messaging.SendEmailResult[getErrors=(Messaging.SendEmailError[getTargetObjectId=null;]);isSuccess=false;]

I've tried this without specifying setTargetObjectId as well, as it should not be necessary given that I am not using a template, but the same error occurs. When I checked the debug log to make sure that the target object and sendTo list were not null, they both appeared correct.

Anyone have ideas?

  • What error did you get when you commented errorMail.setTargetObjectId(UserInfo.getUserId()); out? I feel like you shouldn't have that line in your code without a template. – dphil Sep 11 '14 at 17:27
  • Exactly the same error. I'm just as confused as you, @dphil – LucidLunatic Sep 11 '14 at 17:37
  • What happens when you comment out this line: errorMail.setTargetObjectId(UserInfo.getUserId()); and the debug right below it – greenstork Sep 11 '14 at 17:44
  • This would seem to indicate that the problem could be with a bad email address on one of your recipients: developer.salesforce.com/forums/… – greenstork Sep 11 '14 at 17:51
  • Got it- if body = null, this error was thrown. Which is really bizarre error handling on Salesforce's part. – LucidLunatic Sep 11 '14 at 17:58
1

According to your code snippet you are not using an email template. You just need to send a single email to multiple users. So there is no need of specifying setTargetObjectId(ID). This is an optional which need to be used if using a template. So remove both below lines and retry it.

 errorMail.setTargetObjectId(UserInfo.getUserId());
 System.debug(errorMail.getTargetObjectId());
  • According to the docs, it's actually optional to include regardless of whether or not there is a template. – greenstork Sep 11 '14 at 17:51
1

The problem was that 'body' was null in the cases I was testing. While I'm not sure why Salesforce gives the error it does for this problem, adding a statement along the lines of

if(body == null) body = 'UNKNOWN ERROR';

prior to setting the body of the e-mail solves the issue. Thank you everyone for your help. There are evidently a number of other problems unrelated to the TargetObjectId which can also cause this error, including having an invalid e-mail address in the To or CC fields. Thanks to @greenstork for that one.

1

The following works for me in anonymous apex; note the setSaveAsActivity. I agree with @highfive that setTargetObjectId() only has utility in supporting merge fields within a template and hence for straight plain text email, has no relevance.

Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
Messaging.reserveSingleEmailCapacity(1);
email.setToAddresses(new List<String> {'crop1645@fubar.com'});
email.setCcAddresses(new List<String>{});
email.setbccAddresses(new List<String> {});
email.setSubject('some subject');
email.setPlainTextBody('some body');
email.setTargetObjectId(UserInfo.getUserId());
email.setSaveAsActivity(false);  // required if sending to a user

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.