I have created a Vf email template and batch Apex and trying to send mail to account owner once batch apex execute.I am getting error saying:

INVALID_FIELD_WHEN_USING_TEMPLATE, When a template is specified the plain text body, html body, subject and charset may not be specified

Batch Apex Class :

public class BatchApexUpdateNoOfActiveAccountContact implements Database.Batchable<SObject> {

  public Database.QueryLocator start(Database.BatchableContext context) {
    return Database.getQueryLocator([select Id from Account order by Name]);

  public void execute(Database.BatchableContext context, List<Account> scope) {
    Account[] updates = new Account[] {};
    //Map<Id, Account> mapacc = new Map<Id, Account>();
    List<Id> idofacc = new List<Id>();
    //List<Integer> cpAcc = new List<Integer>();
    //List<Integer> cpCont = new List<Integer>();

    for (AggregateResult ar : [
            select  Account__c a, count(Id) c
            from Contact_Point__c
            where Status__c=true and Account__c in :scope
            group by Account__c
            ]) {
       set<id> ids=new set<id>();
       for(contact com:[SELECT id,firstname,AccountId FROM Contact where accountid =:(Id)ar.get('a')])
    Decimal dec=[Select count() from Contact_Point__c
                    where Status__c=true and Contact__c in :ids];
    updates.add(new Account(id=(Id)ar.get('a'),No_of_active_CP_on_Account__c =(decimal)ar.get('c'),No_of_active_CP_on_Contact__c =dec));
    update updates;

    System.debug('value of updates :::: '+updates);
    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
    Id templateId = [Select id from EmailTemplate where name=:'Contact Point'].id;
    System.debug('value of template ::::: '+templateId);
    Map<Id, String> emailMap = new Map<Id, String>();
    System.debug('value of ID ::: '+idofacc);
    List<Account> ls = [select Id,OwnerId,Name FROM Account where Id in :idofacc];
    List<String> ownid = new List<String>();
    List<String> accName = new List<String>();
    System.debug('value of ID ::: '+ls);
    for(Account acc: ls){
     System.debug('value of accName ::: '+accName);
    System.debug('value of ownid ::: '+ownid);
    List<User> mailid = [Select Id,Email FROM User WHERE Id in :ownid];
        for(User u :mailid){
            emailMap.put(u.Id, u.Email);
    System.debug('value of emailMap ::: '+mailid);

    System.debug('value of emailMap ::: '+emailMap.values());

    mail.setSubject('Updated Count of Contact Points for ' + accName);
    for(Integer i = 0;i<updates.size();i++){
        Account CPonAccount = updates[i];
        Decimal abc = CPonAccount.No_of_active_CP_on_Account__c;
        Decimal pqr = CPonAccount.No_of_active_CP_on_Contact__c;
        System.debug('value of CPonAccount ::: '+CPonAccount);
        System.debug('value of abc ::: '+abc);
        System.debug('value of pqr ::: '+pqr);

        mail.setPlainTextBody('Number of active contact points on Account ' + abc +'\n'+'Number of active contact points on Contact'+ pqr);
        Messaging.sendEmail(New Messaging.SingleEmailMessage[]{mail});



  public void finish(Database.BatchableContext context) {


VF email template :

<messaging:emailTemplate subject="Contact Point Updated" recipientType="User" relatedToType="Account">
  <messaging:htmlEmailBody >
  <apex:image id="accenturelogo" value="https://c.ap2.content.force.com/servlet/servlet.ImageServer?id=0152800000020tH&oid=00D2800000123Fg&lastMod=1444675945000" height="147" width="110"/>
  • VF email page template: <messaging:emailTemplate subject="Contact Point Updated" recipientType="User" relatedToType="Account"> <messaging:htmlEmailBody > <apex:image id="logo" value="c.ap2.content.force.com/servlet/…" height="147" width="110"/> </messaging:htmlEmailBody> </messaging:emailTemplate>
    – Jack vardy
    Oct 14, 2015 at 14:34

2 Answers 2


I'm not familiar with this error specifically, but taking a look at it, it sounds like you either set an email template for your email, or you set email body/subject/charset directly in your Apex code.

This would make sense, given that the template should become the source for your email body, once created.

If that is the case, then I suspect a call in your Apex code to set the body from the Apex class would conflict with trying to set the body via the template.

So my guess is that these calls here:


Is where you are running into problems. You need to do all of that in your template. Instead, put that text along with merge fields into your template, and let Visualforce resolve the Account values for you.

  • I tried by commenting out mail.setPlainTextBody(...); But still i am getting same error. That's strange for me.
    – Jack vardy
    Oct 14, 2015 at 15:28
  • You also have mail.setSubject it looks like.
    – pchittum
    Oct 14, 2015 at 15:30
  • I commented out those lines now getting error saying "INVALID_TYPE_FOR_OPERATION, Only Users, Contact, Lead or Person objects are allowed for targetObjectId : 00128000006bm2h"
    – Jack vardy
    Oct 14, 2015 at 15:49
  • Seeing as the problem with the error you initially reported is addressed, you really should open a new question for this.
    – pchittum
    Oct 14, 2015 at 19:27
  • I got alternate solution rather than going via batch Apex.I used workflow rules and email alerts on field change.I implemented it easily.I created a VF email template with relatedTo set to Account and added subject,Plain text body,Html body inside VF email template.I added a workflow rule for field change and if rule turns to be true it will send mail to account owner with all details.Thanks for all your help. In batch apex there is limitation to send mail per day i.e governor limit for single mail.
    – Jack vardy
    Oct 15, 2015 at 5:42

Target Object should be either Contact , Lead, User when using Template. try using setting Contact Id in Target and check. In Template you did mentioned recipient Type is User but in the Code you are using Account Id as Target Object

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.