function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
BMC GroupBMC Group 

Save error: Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setTreatTargetObjectAsRecipient(Boolean)

Hi everyone,
I am getting an error on below mentioned line when compiling in an apex class but it runs in Execute Anonymous Window :

msg.setTreatTargetObjectAsRecipient(false);

Error : Save error: Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setTreatTargetObjectAsRecipient(Boolean).
Kindly help. Thanks in advance. 
v varaprasadv varaprasad
Hi ,

please check once below link.
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_email_outbound_single.htm#apex_Messaging_SingleEmailMessage_setTreatTargetObjectAsRecipient


setTreatTargetObjectAsRecipient(treatAsRecipient)
Optional. If set to true, the targetObjectId (a contact, lead, or user) is the recipient of the email. If set to false, the targetObjectId is supplied as the WhoId field for template rendering but isn’t a recipient of the email. The default is true.
Signature
public void setTreatTargetObjectAsRecipient(Boolean treatAsRecipient)
Parameters
treatAsRecipient
Type: Boolean


Thanks
Varaprasad
BMC GroupBMC Group
Hi Varaprasad,

Thanks for your response.

I have already gone through the above document but still I'm not able to resolve the error.

Here is my code snippet and I have highlighted the error line -

        
for(Domain_and_Hosting__c tempDH : dhList){
               
            Messaging.SingleEmailMessage msg = new Messaging.SingleEmailMessage();
    
                String[] toAddresses = new list<string>{tempDH.Email__c};  
               
                if(tempDH.CC_Email__c != null && tempDH.CC_Email__c != ''){
 
                     String[] ccAddresses = new list<string>{tempDH.CC_Email__c};
                       ccaddresses = tempDH.CC_Email__c.split(',');  
                       msg.setCcAddresses(ccAddresses);
                }
               
                if(tempDH.Domain_Status__c == 'Domain with FTP Details & Name Server .in' || tempDH.Domain_Status__c == 'Domain with FTP Details & Name Server co.in'){
                     
                       if(attachmentList.size() > 0){
                                                system.debug('---attachmentList---' + attachmentList);
                             
                            for(Attachment tempAttachment : attachmentList){
                                   system.debug('--attachmentList.Name--'+ tempAttachment.Name + '--Content Type-'+ tempAttachment.ContentType);
                                   system.debug('--BODY--' + tempAttachment.Body);
                                 
                                  attachmentName = tempAttachment.Name;
                       
                                     if(attachmentName!=null){
                                         Messaging.EmailFileAttachment emailAttachment = new Messaging.EmailFileAttachment();
                                         emailAttachment.setBody(tempAttachment.Body);
                                         emailAttachment.setContentType(tempAttachment.ContentType);
                                         emailAttachment.setFilename(tempAttachment.Name);
                                         emailAttachments.add(emailAttachment);
                                     }                                     
                            }
                            system.Debug('---attachmentList---'+attachmentList+'---attachmentList size---'+attachmentList.size());
                            }
                }
                  
                msg.setWhatId(tempDH.id);
                msg.setTemplateId(DHSettingMap.get(tempDH.Domain_Status__c));   
                msg.setToAddresses(toAddresses);


             msg.setTreatTargetObjectAsRecipient(false);

                msg.setTargetObjectId(tempDH.TargetID__c); 
                msg.setOrgWideEmailAddressId(orgUserEmailId.get(tempDH.OwnerID));
                if(emailAttachments.size() > 0)
                     msg.setfileAttachments(emailAttachments);
         
                messageList.add(msg);
   
                system.Debug('---messageList---'+messageList);
        }
       
        if(messageList.size()>0)
            Messaging.sendEmail(messageList,false);

msg.setTreatTargetObjectAsRecipient(false); ---> This line is throwing Save error: Method does not exist or incorrect signature: [Messaging.SingleEmailMessage].setTreatTargetObjectAsRecipient(Boolean)

----------------------------------------------------------------------------------
Any further help would be highly appreciated.
Mujtaba Saleforce DevMujtaba Saleforce Dev
Hey mate did u find solution for it. I am finding the same error
IshwarIshwar
setTreatTargetObjectAsRecipient(Boolean treatAsRecipient)
This method was introduced in Winter '16 release. Try updating the API version of your apex class to the latest one. It should work.
https://releasenotes.docs.salesforce.com/en-us/winter16/release-notes/rn_apex_new_classes_methods.htm
 
Kumar PrasannaKumar Prasanna
Thanks much Ishwar!! This worked. I had to update my apex class API version from 34 to the latest one. This solved the issue. I've been stuck with this issue for sometime now. Apriciate your help!