+ Start a Discussion
Pulse CentersPulse Centers 

Send a single email to multiple users with Apex class

I'm trying to add another recipient to an email button and can not seem to get it to work. Below is the Apex code for the current button, please let me know what I need to change to add a second recipient.

global class SendPurchaseOrderEmail {
    WebService static void sendEmail(String poId) {
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        
        String theTemplate = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'Purchase_Order_With_Items'].Id;
        User theUser = [SELECT Id FROM User WHERE Name = 'user name goes here'];

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        mail.setSaveAsActivity(false);
        mail.setTemplateId(theTemplate);
        mail.setWhatId(poId);
        mail.setTargetObjectId(theUser.Id);

        emails.add(mail);
        
        Messaging.sendEmail(emails);      
     
      }    
}
Best Answer chosen by Pulse Centers
Vivek DeshmaneVivek Deshmane
Hi,
Try below code and let me know if it works.
global class SendPurchaseOrderEmail {
    WebService static void sendEmail(String poId) {
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        
        String theTemplate = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'Purchase_Order_With_Items'].Id;
        User theUser = [SELECT Id FROM User WHERE Name = 'user name goes here'];

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        mail.setSaveAsActivity(false);
        mail.setTemplateId(theTemplate);
        mail.setWhatId(poId);
        mail.setTargetObjectId(theUser.Id);
        mail.setToAddresses(new String[] { 'TestUser@salesforce.com' ,'abc@test.com'});  //add other emails here.
        emails.add(mail);
        
        Messaging.sendEmail(emails);      
     
      }    
}

please refer below link for more details.
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_sendemail.htm
Best Regards,
-Vivek

All Answers

Vivek DeshmaneVivek Deshmane
Hi,
Try below code and let me know if it works.
global class SendPurchaseOrderEmail {
    WebService static void sendEmail(String poId) {
        List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
        
        String theTemplate = [SELECT Id FROM EmailTemplate WHERE DeveloperName = 'Purchase_Order_With_Items'].Id;
        User theUser = [SELECT Id FROM User WHERE Name = 'user name goes here'];

        Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
        
        mail.setSaveAsActivity(false);
        mail.setTemplateId(theTemplate);
        mail.setWhatId(poId);
        mail.setTargetObjectId(theUser.Id);
        mail.setToAddresses(new String[] { 'TestUser@salesforce.com' ,'abc@test.com'});  //add other emails here.
        emails.add(mail);
        
        Messaging.sendEmail(emails);      
     
      }    
}

please refer below link for more details.
https://developer.salesforce.com/docs/atlas.en-us.api.meta/api/sforce_api_calls_sendemail.htm
Best Regards,
-Vivek
This was selected as the best answer
Pulse CentersPulse Centers
Thanks Vivek, That did the trick.
Aladdin USAladdin US
Hi

        String theTemplate = [SELECT Id FROM EmailTemplate WHERE DeveloperName ='Purchase_Order_With_Items'].Id;
in the query from where did you get the value ' DeveloperName ='Purchase_Order_With_Items'].Id; '

Thank You.