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')])
{
ids.add(com.id);
}
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));
idofacc.add((Id)ar.get('a'));
//cpAcc.add((decimal)ar.get('c'));
//cpCont.add(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){
ownid.add(acc.OwnerId);
accName.add(acc.Name);
}
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.setToAddresses(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.setTargetObjectId(CPonAccount.Id);
mail.setSaveAsActivity(false);
mail.setTemplateId(templateId);
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"/>
</messaging:htmlEmailBody>
</messaging:emailTemplate>