[SalesForce] Messaging.sendEmail() returns an error that the target object Id is null when it was set

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?

Best Answer

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

Related Topic