function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
nfon teamusernfon 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.
nfon teamusernfon teamuser
An update, I have just noticed, the problem is in the TargetObjectID, this code works and send 10 emails

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);
 
Code+1Code+1
Hi nfon teamuser,

 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});
nfon teamusernfon teamuser
Hi,
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.