I have a custom button to send an email. The code is working fine. Now I would like to insert those email in the activity history. I did set mail.setSaveAsActivity(true). But when I click the button it is showing the error as:

enter image description here

I am unable to resolve this.Any help is appreciated. The following is my code.

global class memberApproval {

webservice static boolean callApproval(Id localId) { 
            set<Id> Uid = new set<Id>();
            String grpId = Label.GroupId;
     List<GroupMember> Lst =[Select Id, UserOrGroupId From GroupMember Where GroupId =: grpId];
     opportunity opp =[select id,owner.email from opportunity where Id =:localId];
     for(GroupMember g:Lst){

     List<User> Lstuser = [select id,email from user where Id In:Uid];

     List<String> EmailIds = new List<string>();

     for(User u:Lstuser){
       //New instance of a single email message
         Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

        // Who you are sending the email to

           mail.setToAddresses( EmailIds  );
        String templateIdVal= Label.TemplateId;
       String[] bccEmail = new String[]{opp.owner.email};

           // The email template ID used for the email
           mail.setSenderDisplayName('Confirmation of Order Quote');

        Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
    return true;               


  • Updated my answer. Jun 16, 2015 at 20:11

1 Answer 1


This argument setBccAddresses and setToAddresses is allowed only when a template is not used. Please remove that and try again. setTargetObjectId will itself resolve the recipient list based on the email template you have provided.

  • Emails are working perfectly using the template. Both the to address recipient and BCC recipient are getting emails. My issue is I would like to add this email to activity history so that user will make a note of it if he checks in the activity history before sending another email. Jun 16, 2015 at 20:16
  • I understand your point. I am saying, if you are using templateId, then ONLY use targetObjectId, which accepts ID of the contact, lead, or user to which the email will be sent. setSaveAsActivity will work only when targetObjectId is used. If not using a template, then you should use setHTMLbody to create the body and setToAddresses to create recipient list. Therefore I told you to either use targetObjectId OR setToAddresses, but not both together. This is may be causing the error. Jun 16, 2015 at 20:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .