You need to sign in to do that
Don't have an account?
SV M
Email attachment is not able to read merge field values
Hi, I have a batch class which will send email to contact owner along with attachment with some merge fields. I was able to send the attachment but the merge fields are not taking values from Contact object. Can someone help me to resolve this?
//Batch Class
global class EmailWithAttachment implements Database.Batchable <sObject>, Database.Stateful{
public String Subject{get;set;}
//Public String Body{get;set;}
Public String attname{get;set;}
Public blob attbody{get;set;}
Map<Id,String> contactOwnerEmail = new Map<Id,String>();
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT FirstName, LastName, Email, MobilePhone FROM Contact';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Contact> scope) {
/*for(Contact con : scope) {
contactOwnerEmail.add(con.Id, con.Owner.Email);
}*/
}
global void Finish(Database.BatchableContext BC) {
messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
EmailTemplate ev=[SELECT Id, Name, Subject, Body, HtmlValue
FROM EmailTemplate
WHERE name = 'Test Template'];
Blob b = Blob.valueOf(ev.Body);
Subject = ev.Subject;
//Body = ev.Body;
email.setSubject(Subject);
email.setPlainTextBody('Please Find the Attachment for Details about Contact');
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(attname);
efa.setBody(b);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
email.setToAddresses(new String[]{'maddulasaivineeth@gmail.com'});
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
At the setToAddress, I need to use Contact Owner Email but I was unable to get the Contact Owner Email. I have attached the image for your reference.
//Batch Class
global class EmailWithAttachment implements Database.Batchable <sObject>, Database.Stateful{
public String Subject{get;set;}
//Public String Body{get;set;}
Public String attname{get;set;}
Public blob attbody{get;set;}
Map<Id,String> contactOwnerEmail = new Map<Id,String>();
global Database.QueryLocator start(Database.BatchableContext BC) {
String query = 'SELECT FirstName, LastName, Email, MobilePhone FROM Contact';
return Database.getQueryLocator(query);
}
global void execute(Database.BatchableContext BC, List<Contact> scope) {
/*for(Contact con : scope) {
contactOwnerEmail.add(con.Id, con.Owner.Email);
}*/
}
global void Finish(Database.BatchableContext BC) {
messaging.SingleEmailMessage email = new messaging.SingleEmailMessage();
EmailTemplate ev=[SELECT Id, Name, Subject, Body, HtmlValue
FROM EmailTemplate
WHERE name = 'Test Template'];
Blob b = Blob.valueOf(ev.Body);
Subject = ev.Subject;
//Body = ev.Body;
email.setSubject(Subject);
email.setPlainTextBody('Please Find the Attachment for Details about Contact');
Messaging.EmailFileAttachment efa = new Messaging.EmailFileAttachment();
efa.setFileName(attname);
efa.setBody(b);
email.setFileAttachments(new Messaging.EmailFileAttachment[] {efa});
email.setToAddresses(new String[]{'maddulasaivineeth@gmail.com'});
Messaging.sendEmail(new Messaging.SingleEmailMessage[] {email});
}
}
At the setToAddress, I need to use Contact Owner Email but I was unable to get the Contact Owner Email. I have attached the image for your reference.