An apex class is triggered by a process builder when a field value is changed. I am trying to send an email to all owners of records of a related object and I get this error. It works when there is only one record when there are multiple records I get this error.
global class sendPaymentNotification {
global class dataWrapper {
@InvocableVariable(label='Insert InvoiceId' required=true)
public String InvoiceId;
@InvocableVariable(label='Email Template Id' required=true)
public String emailTemplateId;
}
@InvocableMethod(Label='Send Payment Received Email to User')
global static void sendEmail(List<dataWrapper> passedData) {
//Send Email
//
//set Ids
Set<Id> userIds = new Set<Id>();
for(dataWrapper dw: passedData) {
//Get the Template id for email
EmailTemplate selectedTemplate = [SELECT Id FROM EmailTemplate WHERE Id =: dw.emailTemplateId];
//query invoice distributions
//
//
//get invoiceDist emails
//
Invoice_Distribution__c pull = [SELECT OwnerId FROM Invoice_Distribution__c where Invoice__c = :dw.InvoiceId and recordowner__c != ''];
//Create Email object and send
Messaging.SingleEmailMessage mailToSend = new Messaging.SingleEmailMessage();
//bring in object Id of Invoice Distributions for emailing
mailToSend.setTargetObjectId(Pull.OwnerId);
mailToSend.setTemplateId(selectedTemplate.Id);
mailToSend.setSaveAsActivity(false);
List<Messaging.SendEmailResult> sendResults = Messaging.sendEmail(new List<Messaging.Email> { MailToSend });
System.debug(sendResults[0]);
}
}
}