0

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];
     if(Lst.size()>0){
     opportunity opp =[select id,owner.email from opportunity where Id =:localId];
     for(GroupMember g:Lst){
         Uid.add(g.UserOrGroupId);
     }

     if(Uid.size()>0){
     List<User> Lstuser = [select id,email from user where Id In:Uid];
     if(Lstuser.size()>0){

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


     for(User u:Lstuser){
     EmailIds.add(u.Email);
     }
       //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.setTemplateId(templateIdVal);
           mail.setTargetObjectId(userinfo.getuseriD());
           mail.setwhatid(opp.id);
           mail.setBccSender(true);
           mail.setBccAddresses(bccEmail);
           mail.setUseSignature(false);
           mail.setReplyTo(opp.owner.email);
           mail.setSenderDisplayName('Confirmation of Order Quote');
           mail.setSaveAsActivity(true);  

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

}

  • Updated my answer. – the_phantom Jun 16 '15 at 20:11
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.

|improve this answer|||||
  • 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. – SFDC_BigDog Jun 16 '15 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. – the_phantom Jun 16 '15 at 20:28

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.