0

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.

3
  • I'm not following what's the desired result. Looking at the code, it seems you'd send an email for every Renewal__c pulled in the query. You're also hard-coding IDs which I'd strongly suggest you avoid. Commented Oct 25, 2019 at 13:12
  • A lot of context is missing here. For example, you are sending the emails to ren.Family__r.Email_Id__c. Have you confirmed that all the Renewals are to different Families, or that the families have different email addresses? Also, the code is highly inefficient. There are a lot of queries nested inside code that is called within loops. You are also querying the renewals again in sendemail2, even though you are already querying them in SendBulkrenewalEmail. You shouldn't pass the Id just to query the record again. Pass in the record itself. And don't loop through and call...
    – Roti
    Commented Oct 25, 2019 at 17:45
  • Messaging.sendEmail for each individual Renewal. The Messaging.SingleEmailMessage objects should all be added to a list, and that list should then be passed to Messaging.sendEmail one time. Also make sure that you are not adding duplicates to the list. And you don't seem to actually be doing anything with rpRecord. Is it just being used for the VF page? If so, you are clearing it repeatedly through the loop, so I'm not sure what you're looking to show with it. These are just some things that I noticed, but I'm not sure what you're even trying to accomplish here. I don't know the data model.
    – Roti
    Commented Oct 25, 2019 at 17:49

1 Answer 1

1

The problem you're having here is you cannot send different attachments to individual recipients of the same (common) bulk email. Either they all receive the same email with the same attachment or they do not. To do what you want to do, you'd need to have a marketing tool from Marketing Cloud like Journey Builder. You're attempting to send the equivalent of a "mail merge" as a single bulk email which is a feature that Salesforce does not support using bulk messaging methods.

1
  • Or do in a batchable or chained queueable
    – cropredy
    Commented Oct 26, 2019 at 14:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .