0

I am trying to send a HTML Email Template with an attached PDF, using this scheduler class. I receive two emails one with Attachment and the other is just the text without Attachment(not a text version).

global class pS implements Database.batchable<sObject> , Database.Stateful{

private EmailTemplate emailTemp;
global pS(){
    emailTemp = [Select Id From EmailTemplate Where Name = 'SP']; 
}

global Database.QueryLocator start(Database.BatchableContext BC){
    String query = 'Select Id, Lastname, Firstname, Email From Contact WHERE ((Account.RecordType.Id = \'01239000000N6dy\' AND Account.premier__c = False) OR (RecordType.Id = \'01239000000N6e1\' AND Account.Status__c = \'Processing\')) AND Account.Name != \'Test\' AND Email != null AND HasOptedOutOfEmail = False';
    return Database.getQueryLocator(query);
}

global void execute(Database.BatchableContext BC, List<Contact> scope){           

    List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for(Contact con : scope) {
         if(emailTemp != null && con.Email != null && con.Email != '') {
           Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
           email.setToAddresses(new List<String>{con.Email}); 
           email.setTargetObjectId(con.Id);                   
           email.setTemplateId(emailTemp.Id);
           email.setTreatTargetObjectAsRecipient(true);        
           emails.add(email);                                      
        }          
    Messaging.sendEmail(emails);
    }                
}   
global void finish(Database.BatchableContext BC){
}
}

Can someone help me with this, thanks.

1
  • I don't see where you're attaching a file to the outgoing messages at all. Also, true is the default for setTreatTargetObjectAsRecipient(), so you don't need to do that, and you don't need to add the target ID and also a To address for the same contact.
    – David Reed
    Mar 9, 2018 at 0:53

1 Answer 1

1

You're sending emails inside for loop.

List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
    for(Contact con : scope) {
         if(emailTemp != null && con.Email != null && con.Email != '') {
           Messaging.SingleEmailMessage email = new Messaging.SingleEmailMessage();
           email.setToAddresses(new List<String>{con.Email}); 
           email.setTargetObjectId(con.Id);                   
           email.setTemplateId(emailTemp.Id);
           email.setTreatTargetObjectAsRecipient(true);        
           emails.add(email);                                      
        }          
    Messaging.sendEmail(emails); ------> this needs to be below curly bracket.
    }  

If your batch size is bigger than 1 this batch class would send multiple emails for same contacts.

Let's say your batch contains 3 contacts named A, B, C.

First you send email to A. Then you send email to A and B. Then you send email to A, B and C. Just move send.email line below curly bracket.

Also your code does not have attachment part.

2
  • Awesome @tugce! worked like a charm, thank you so much! We are attaching the PDF manually to the email template, because it keeps changing every week.
    – Lin
    Mar 9, 2018 at 18:33
  • Okay. Glad it helped!
    – tugce
    Mar 10, 2018 at 6:47

You must log in to answer this question.

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