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
SFDC RocksSFDC Rocks 

Apex mail API question

When the below Apex code executes, I receive the below error message.  If I comment out the two " setSaveAsActivity" lines, I don't get this error message.   I cannot recall the history for why this Apex code is written this way, but do I need to be making these mail.setSaveAsActivity(false) calls?

 

===

 

ERROR MESSAGE:

 

internal error [SendEmail failed. First exception on row 1; first error: INVALID_FIELD_WHEN_USING_TEMPLATE, When a template is specified the plain text body, html body and subject may not be specified ].

 

===

CODE:

 

            Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

                       

            mail.setTemplateId(templateId);

            mail.setWhatId(vaId);

            mail.setSaveAsActivity(false);

            mail.setUseSignature(true);

            mail.setSenderDisplayName(System.Label.EmailSenderDisplayName);

            mail.setSaveAsActivity(false);

           

            List<Messaging.SingleEmailMessage> messages = new List<Messaging.SingleEmailMessage>();

           

            for (Id recipient:recipients)

            {

                mail.setTargetObjectId(recipient);

                messages.add(mail);

            }

           

            Messaging.sendEmail(messages);

 

===