3

I don't understand why I would get the following error when in code it is being set and when I use a debug statement to log the SingleEmailMessage, it show a valid value for the field.

            System.debug( 'BDM = ' + BDM );
        msg = new Messaging.SingleEmailMessage();
        msg.setTemplateID( template.Id );
        msg.setWhatId( cas.Id );
        if( BDM != null ){
            msg.setTargetObjectId( BDM.Id );
        }
        else{
            // send it to a queue?
            // msg.setToAddress( );
        }
        toSend.add( msg );
    }
    if( toSend.size() > 0 ){
        System.debug( 'send the e-mail ' + toSend );
        List<Messaging.SendEmailResult> results = Messaging.sendEmail( toSend, false );
        System.debug( results );
    }

The log shows: 08:07:00.568 (568505000)|USER_DEBUG|[60]|DEBUG|BDM = User:{Name=Robert Harper, Email=robert.harper@aruplab.com, Id=005C00000052U2zIAE} 08:07:00.568 (568510000)|SYSTEM_METHOD_EXIT|[60]|System.debug(ANY) 08:07:00.569 (569836000)|SYSTEM_METHOD_ENTRY|[71]|LIST.add(Object) 08:07:00.569 (569877000)|SYSTEM_METHOD_EXIT|[71]|LIST.add(Object) 08:07:00.569 (569892000)|SYSTEM_METHOD_ENTRY|[46]|system.ListIterator.hasNext() 08:07:00.569 (569916000)|SYSTEM_METHOD_EXIT|[46]|system.ListIterator.hasNext() 08:07:00.569 (569938000)|SYSTEM_METHOD_ENTRY|[73]|LIST.size() 08:07:00.569 (569965000)|SYSTEM_METHOD_EXIT|[73]|LIST.size() 08:07:00.570 (570000000)|SYSTEM_METHOD_ENTRY|[74]|String.valueOf(Object) 08:07:00.570 (570107000)|SYSTEM_METHOD_EXIT|[74]|String.valueOf(Object) 08:07:00.570 (570127000)|SYSTEM_METHOD_ENTRY|[74]|System.debug(ANY) 08:07:00.570 (570142000)|USER_DEBUG|[74]|DEBUG|send the e-mail (Messaging.SingleEmailMessage[getBccAddresses=null;getCcAddresses=null;getCharset=null;getDocumentAttachments=null;getFileAttachments=null;getHtmlBody=null;getInReplyTo=null;getOrgWideEmailAddressId=null;getPlainTextBody=null;getReferences=null;getTargetObjectId=005C00000052U2zIAE;getTemplateId=00Xc0000000I6KiEAK;getToAddresses=null;getWhatId=500c0000002IHIRAA4;isUserMail=false;]) 08:07:00.570 (570155000)|SYSTEM_METHOD_EXIT|[74]|System.debug(ANY) 08:07:00.570 (570187000)|SYSTEM_METHOD_ENTRY|[75]|Messaging.sendEmail(LIST, Boolean) 08:07:00.609 (609596000)|SYSTEM_METHOD_EXIT|[75]|Messaging.sendEmail(LIST, Boolean) 08:07:00.609 (609658000)|SYSTEM_METHOD_ENTRY|[76]|System.debug(ANY) 08:07:00.609 (609740000)|USER_DEBUG|[76]|DEBUG|(Messaging.SendEmailResult[getErrors=(Messaging.SendEmailError[getTargetObjectId=null;]);isSuccess=false;])

What am I doing wrong?

1 Answer 1

4

This error is because the method msg.setTargetObjectId(BDM.Id ) uses User Id and as per documentation you need to use msg.setWhatId(cas.Id) only when setTargetObjectId is contact.

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_Messaging_SingleEmailMessage_setWhatId.htm

I faced same issue yesterday

3
  • So then can I not have the email go to a user when using a template for a Case record? Dec 17, 2013 at 15:42
  • You cannot send email to user using the standard template.Workaround is to create visualforce email template and then you can send an email to user
    – sfdc
    Dec 17, 2013 at 15:48
  • Does the contact have to be the same as the information in the Case? In other words, can I set the target Id to a different contact? Dec 17, 2013 at 16:33

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.