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
Cheerag VijayaCheerag Vijaya 

Send e-mail

Hi! When I click a button on the VF page, I need to send e-mail to 4 members and the e-mail body should be same for all the 4 members. What can I do ?
Harish RamachandruniHarish Ramachandruni
hi,
 
public PageReference send(List<Account> accountIds)
    {
        Map<Id, Contact> contactMap = new Map<Id, Contact>([SELECT Id, Email FROM Contact WHERE AccountId IN :accountIds]);

        EmailTemplate template =  [SELECT Id, Name FROM EmailTemplate WHERE DeveloperName = 'My_Unique_API_Name' LIMIT 1];

        Messaging.MassEmailMessage emails=new Messaging.MassEmailMessage(); 
        emails.setTargetObjectIds(contactMap.keySet());
        emails.setTemplateId(template.Id);
        emails.setsubject('note');
        Messaging.SendEmail(New Messaging.MassEmailMessage[]{emails});

        return null;
    }

 
Lance Shi 8Lance Shi 8
Harish's code pretty much demonstrate what you need to do here. To send emails to multiple contacts, simple put their contact Id as a String list and use that as a parameter passed into emails.setTargetObjectIds(). Then you will be able to send the emails.