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
PegazysPegazys 

INVALID_SAVE_AS_ACTIVITY_FLAG, saveAsActivity must be false when sending mail to users

Here is portion of my code,

 

        userId = Userinfo.getUserId();

 

        email.setTargetObjectId(userId);
        email.setBccSender(true);
        email.setSaveAsActivity(true);
        email.setUseSignature(false);
        email.setSubject(subject);
        email.setToAddresses(sendToAddresses);
        email.setPlainTextBody(body);

 

Everything works fine when I set setSaveAsActivity to false. But I want to save it as an Activity when email is sent.

When I set setSaveAsActivity to true, I get the error, INVALID_SAVE_AS_ACTIVITY_FLAG, saveAsActivity must be false when sending mail to users

 

Does this have anything to do with which Id I am using to set in setTargetObjectId?

 

Can any of you help with this?

psevakpsevak

kindly add following line into your code

 

 email.saveAsActivity = false;

 

thanks,


 


Baird_SBaird_S

Correction - that's

 

mail.saveAsActivity = false;

David de LeonDavid de Leon
You have to set the TargetObjectId to a contact, rather than a user.

        email.setTargetObjectId(contactId);

This error will appear when you are trying to save the email as an activity, but are sending to a user. If you have a contact created for the user you could use that, if not another option is to use a generic contact and set the user email in the setToAdresses, just as you're doing.
David de LeonDavid de Leon
Reference:
https://developer.salesforce.com/forums/?id=9060G000000XamlQAC