I have been working on Messaging class where I have to fetch records and need to send indivisual customer mail Vf page as PDF with their individual records. When I get this records and put into the list and that list I am sending to the particular emails. It is going with all the records of that list of every users even I sending in the loop. Please look my class below!
public class SendBulkrenewalEmail {
public Id rpid {get;set;}
public Renewal__c rRecord {get; set;}
public List<Renewal_Product__c> rpRecord {get;set;}
public Map<Id, List<Renewal_Product__c>> mapIdprod {get;set;}
public SendBulkrenewalEmail(){
rpRecord = new List<Renewal_Product__c> ();
mapIdprod = new Map<Id, List<Renewal_Product__c>>();
for(Renewal__c ren : [Select Id, name, Family__r.name, Family__r.Email_ID__c From Renewal__c where Id IN ('a180w0000008r5lAAA','a180w000000CasHAAS')]){
sendmail(ren.Id, ren.Family__r.Email_ID__c);
}
}
public void sendmail(Id renId, String emailid){
system.debug('***!--'+renId+'----'+emailid);
rpRecord.clear();
system.debug('*2*!--'+rpRecord);
for(Renewal_Product__c rpr : [SELECT ID,Name,Premium_Due_Date__c,Renewal__c,Company__r.Name,Client_Name__r.Name
FROM Renewal_Product__c
Where Renewal__c =: renId]){
rpRecord.add(rpr);
}
system.debug('*3*!--'+rpRecord);
sendmail2(renId, emailid);
}
public void sendmail2(Id renId2, String emailid2){
system.debug('*4*!--'+rpRecord);
for(Renewal__c ren : [Select Id, name, Family__r.name, Family__r.Email_ID__c From Renewal__c where Id =: renId2]){
system.debug('*3*!--'+ren);
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
PageReference pdf = Page.SendBulkrenewalEmailpage;
pdf.getParameters().put('id',ren.Id);
Blob body;
try {
body = pdf.getContent();
} catch (VisualforceException e) {
body = Blob.valueOf('Some Text');
}
Messaging.EmailFileAttachment attach = new Messaging.EmailFileAttachment();
attach.setContentType('application/pdf');
attach.setFileName('testPdf.pdf');
attach.setInline(false);
attach.Body = body;
mail.setUseSignature(false);
mail.setToAddresses(new String[] {ren.Family__r.Email_ID__c});
mail.setSubject('PDF Email Demo');
mail.setHtmlBody('Here is the email you requested! Check the attachment!');
mail.setFileAttachments(new Messaging.EmailFileAttachment[] { attach });
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
system.debug('*3mail*!--'+mail);
}
}
}
Please help me in this.