1

I am using a batch class with 4 differnt VF templates to send emails to 4 different regional heads usingsingleemailmethod() so I am using 4/10 singleemail methods in the batch class.

singleemail supports settargetId() and not settargetIds() as massemail, which makes it hard to specify multiple Id's too :(

http://www.salesforce.com/us/developer/docs/apexcode/Content/apex_classes_email_outbound_single.htm

singleemailmethod does not support setbcc,setcc etc... when used with a templateID, so I thought i would look into massemailmessage() but the problem there is It does not support Vf templates.

INVALID_ID_FIELD, Visualforce templates are not currently supported for mass email.: []

I cannot use what Id too here because I have 4 different templates which needs to be used based on region,making it hard to run all this in a for loop.

Am I doing something totally off that can be corrected,I want to cc/bcc the email templates to some admin users who will get copy of all the emails that is being sent over from this class.How do I copy a template to multiple users?

code:

global class Schedule_report implements Schedulable
{
    global void execute(SchedulableContext ctx)
    {
        list<EmailTemplate> lstEmailTemplates = [select Id,Name,DeveloperName from EmailTemplate where( Name=: 'NA' OR Name=: 'ASIA_Pacific' OR Name=: 'Europe' OR Name=: 'Japan')];

        map<string,string> emailmap = new map<string,string>();
        if(lstEmailTemplates!=null && lstEmailTemplates.size()>0)
        {
            for(emailtemplate e: lstEmailTemplates){
            emailmap.put(e.name,e.id);
            }
        }

        if(emailmap.containsKey('NA')){
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setTargetObjectId('005d0000001TT4P');
                mail.setTemplateId(emailmap.get('NA'));
                mail.setSaveAsActivity(false);
                Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
       }
        if(emailmap.containsKey('ASIA_Pacific')){
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setTargetObjectId('005d0000001TT4Q');
                mail.setTemplateId(emailmap.get('ASIA_Pacific'));
                mail.setSaveAsActivity(false);
                Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
       }
        if(emailmap.containsKey('Europe')){
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setTargetObjectId('005d0000001TT4R');
                mail.setTemplateId(emailmap.get('Europe'));
                mail.setSaveAsActivity(false);
                Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
       }

       if(emailmap.containsKey('Japan')){
                Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
                mail.setTargetObjectId('005d0000001TT4S');
                mail.setTemplateId(emailmap.get('Japan'));
                mail.setSaveAsActivity(false);
                Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
       }

    }
2

Well, you can only call sendEmail 10 times in a context. But each call can contain multiple messages. You have it coded to only use a single entry in the list for each send So where you have

Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });

You could have something like

List<Messaging.SingleEmailMessage> mailsToSend = new List<Messaging.SingleEmailMessage>();
for (// blah blah loop stuff
    mailsToSend.add(mail);
}
// and then 
Messaging.SendEmailResult[] resultMail = Messaging.sendEmail(mailsToSend);
  • Thanks that helped.Was one of those days where my brain froze and never had any juice left to think :) – Rao May 30 '13 at 17:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.