You need to sign in to do that
Don't have an account?

HTML Email template Merge fields are not working in Batch class
Hello ,
Based on the requirement i have created few html email templates with the merge fields .When i use the templateid in the batch class and execute the batch class,the email which i get doesnot display the merge fields value .What may be issue .Any help very much appreciated.
Here is the code used to send an email notification for the 30 days .
Batch Class and Schedule Class :

The output which i get without the merge fields is :

Any help very much appreciated.
Based on the requirement i have created few html email templates with the merge fields .When i use the templateid in the batch class and execute the batch class,the email which i get doesnot display the merge fields value .What may be issue .Any help very much appreciated.
Here is the code used to send an email notification for the 30 days .
Batch Class and Schedule Class :
global class ThirtyDaysNotificationEmail implements Database.Batchable < sObject >, Schedulable, Database.Stateful { global List<String> errorMessages = new List<String>(); global Database.QueryLocator start(Database.BatchableContext bc) { Date ed = Date.today().addDays(30); System.debug(Date.today().addDays(30)); set<Id> setContractIds = new set<Id>(); for(Contract_role__c objContract: [SELECT Contract__c FROM Contract_role__c WHERE Role__c = 'SA' AND Contract__r.EndDate =: ed]) { setContractIds.add(objContract.Contract__c); } return Database.getQueryLocator([Select id, Contract_Name__c , EndDate ,Contact_Email__c, Contract_End_Date_2__c, Account.Owner.Email ,Account.Owner.Manager.Email,Owner.Email,CustomerSignedId,OwnerId FROM Contract WHERE Id IN: setContractIds AND SRR__c ='YES' AND Status__c IN('In Pipe','Pushed Forward ') AND Renewal_Status__c NOT IN ('Renewed','Renewed Lost')]); } global void execute(Database.BatchableContext bc, List < Contract > recs) { List < Messaging.SingleEmailMessage > mailList = new List < Messaging.SingleEmailMessage > (); for (Contract c: recs) { if (c.Contact_Email__c != null) { List < String > toAddresses = new List < String > (); List < String > CcAddresses = new List < String > (); Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); toAddresses.add(c.Contact_Email__c); ccAddresses.add(c.Account.Owner.Email); // toAddresses.add(c.Account.Owner.Manager.Email); mail.setToAddresses(toAddresses); mail.setCcAddresses(CcAddresses); // mail.setTargetObjectId(c.CustomerSignedId); mail.setTargetObjectId(c.OwnerId); // mail.setWhatId(c.Id); mail.setTemplateId('00X4B000000M3g7'); mail.setSaveAsActivity(false); // mail.setSubject('Notification Before 100 Days of Contract End Date to Account Executive and Manager'); // String messageBody = '<html><body>Hi ,The Contract Named ' + c.Contract_Name__c + ',<br>Will get Expired within 100 Days . <br>Kindly take approriate action to inform the Customer.<br><br><b>Regards,</b><br>ADP</body></html>'; // mail.setHtmlBody(messageBody); mailList.add(mail); } } Messaging.sendEmail(mailList); } global void finish(Database.BatchableContext bc) { AsyncApexJob aaj = [Select Id, Status, NumberOfErrors, JobItemsProcessed, MethodName, TotalJobItems, CreatedBy.Email from AsyncApexJob where Id =:BC.getJobId()]; // Send an email to the Apex job's submitter notifying of job completion. Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage(); String[] toAddresses = new String[] {aaj.CreatedBy.Email}; mail.setToAddresses(toAddresses); mail.setSubject('JOB Salesforce Thirty Days NotificationEmailtoCustomer Finished: ' + aaj.Status); String bodyText='Total Job Items ' + aaj.TotalJobItems + ' Number of records processed ' + aaj.JobItemsProcessed + ' with '+ aaj.NumberOfErrors + ' failures.\n'; bodyText += 'Number of Error Messages ' + errorMessages.size() + '\n'; bodyText += 'Error Message' + String.join(errorMessages, '\n'); mail.setPlainTextBody(bodyText); Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail }); } global void execute(SchedulableContext SC) { NotificationEmailtoAccountExecutive batchable = new NotificationEmailtoAccountExecutive(); database.executebatch(batchable); } }HTML Email template Created :
The output which i get without the merge fields is :
Any help very much appreciated.
http://salesforce.stackexchange.com/questions/118857/sendemail-failed-required-field-missing-missing-targetobjectid-with-template/118876#118876
' +'Your current tentative blocks are as under with the respective block release dates.
' +'
Please let us know of the payment and contract details in order to regularize & confirm the same on the system. In the event of the same not being regularized, the block will be automatically released from the system and further rooms will be subject to availability.' +'
Thank You.
' +'Regards
' +'SanjeeviKumar G'); Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail}); system.debug('@@@@@@@@@@@@@@@mail@@@@@@@@@@@@@@@'+mail); c.Email_Sent_Test__c = TRUE; lstNMBReq.add(c); } if(lstNMBReq.size() > 0){ update lstNMBReq; } } } global void finish(Database.BatchableContext BC) { } } Regards, *Hareesh*
http://salesforce.stackexchange.com/questions/119599/test-class-is-not-getting-covered-messaging-singleemailmessage-in-batch-class/119664#119664