You need to sign in to do that
Don't have an account?
nfon teamuser
SingleEmailMessage send only the last message from list
Hi I have noticed a problem with the Messaging.SingleEmailMessage method. I have this anonymous test code
Messaging.SingleEmailMessage[] emails = new List<Messaging.SingleEmailMessage>();
integer i = 0;
while(i < 10)
{
i++;
Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage();
mail2.setToAddresses(new String[] {'xxx'});
mail2.setReplyTo('xxx');
mail2.setSenderDisplayName('Test');
mail2.setTemplateId('00XL0000000EBG4');
mail2.setTargetObjectId('003D000000hv4sd');
mail2.setWhatId('a0fL0000004foqp');
emails.add(mail2);
}
Messaging.sendEmail(emails);
In the console I see, that this message is added 10 times to the queue, but I receive only one message and only one message is created in salesforce in the activity history.
In the main code I create emails for different target object and the emails are different inside, but I use the same template for all of them. I have noticed, that only the last email I added to the list is send, the other ones are ignored. This happens only if I use an Template, the I set the body 10 emails are delivered as the should be.
Any suggestions?
Thanks.
Messaging.SingleEmailMessage[] emails = new List<Messaging.SingleEmailMessage>();
integer i = 0;
while(i < 10)
{
i++;
Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage();
mail2.setToAddresses(new String[] {'xxx'});
mail2.setReplyTo('xxx');
mail2.setSenderDisplayName('Test');
mail2.setTemplateId('00XL0000000EBG4');
mail2.setTargetObjectId('003D000000hv4sd');
mail2.setWhatId('a0fL0000004foqp');
emails.add(mail2);
}
Messaging.sendEmail(emails);
In the console I see, that this message is added 10 times to the queue, but I receive only one message and only one message is created in salesforce in the activity history.
In the main code I create emails for different target object and the emails are different inside, but I use the same template for all of them. I have noticed, that only the last email I added to the list is send, the other ones are ignored. This happens only if I use an Template, the I set the body 10 emails are delivered as the should be.
Any suggestions?
Thanks.
Messaging.SingleEmailMessage[] emails = new List<Messaging.SingleEmailMessage>();
integer i = 0;
List<Id> targets = new List<Id> {'003D000000hv4sd', '0032000000f7U8E', '0032000000T6rER', '0032000000SyLEo', '0032000000HRCVs', '0032000000TtZ7t', '0032000000Uyk9L', '003D000000grUbJ', '0032000000HzkAc', '003D000000qv9Xx', '0032000000bq29u' };
for(id tt :targets)
{
i++;
Messaging.SingleEmailMessage mail2 = new Messaging.SingleEmailMessage();
mail2.setToAddresses(new String[] {'josef.kral@nfon.net'});
mail2.setReplyTo('salesforceadmin@nfon.net');
mail2.setSenderDisplayName('Test');
mail2.setTemplateId('00XL0000000EBG4');
mail2.setTargetObjectId(tt);
mail2.setWhatId('a0fL0000004foqp');
emails.add(mail2);
}
Messaging.sendEmail(emails);
Instead of Messaging.SingleEmailMessage[], please try using Messaging.MassEmailMessage[]..
Try the below sample code --
Trigger:
public List<Id> contactIds = new List<Id>();
for(Contact con : trigger.New ) {
contactIds.add( con.OwnerId);
}
Messaging.MassEmailMessage mail = new Messaging.MassEmailMessage();
mail.setTargetObjectIds(contactIds);
mail.setTemplateID('00XL0000000EBG4');
mail.setSaveAsActivity(false);
Messaging.sendEmail(new Messaging.MassEmailMessage[] {mail});
we are using visual force template and
"Visualforce templates are not currently supported for mass email."
I have found a workaround, that every tagetobjectID in a single batch of emails needs to be unique. If there are 2 and more emals with the same target boject ID, only the last one is send.