There is a field on the Contact object which can have multiple email addresses in it. If there is only 1 address in the field, I can send it out using the APEX trigger. If there are multiple I get the error: INVALID_EMAIL_ADDRESS, Invalid to address : [email protected], [email protected]: []: Trigger.ClientCare_TeamTrainingDateChange: line 64, column 1 I've tried separating the address with semicolons, putting single quotes around each email, double quotes, etc. I've tried to create the String in the setCCAddress, outside of the setCCAddress, etc. Everything works with 1 address, nothing works with more than 1.
If I hard code the address in my APEX it work, but I need to be able to use the data in the field.
trigger ClientCare_TeamTrainingDateChange on Contact (before update) {
List<Task> newTasks = new List<Task>();
List<Messaging.SingleEmailMessage> emails = new List<Messaging.SingleEmailMessage>();
Map<Id,Contact> newContactMap = Trigger.newMap;
Map<Id,Contact> oldContactMap = Trigger.oldMap;
for(Id contactId:newContactMap.keySet()){
Contact myNewContact = newContactMap.get(contactId);
Contact myOldContact = oldContactMap.get(contactId);
if (myOldContact.Team_Training_Start__c != NULL && myNewContact.Stage__c == 'Client' && myNewContact.Status__c == 'Active' && myNewContact.RecordTypeId == '012d0000000t7JQ' && (myNewContact.Program__c == 'Open Doors' || myNewContact.Program__c == 'Canine Magic' || myNewContact.Program__c == 'Project HEAL'))
{
if (myNewContact.Team_Training_Start__c <> myOldContact.Team_Training_Start__c)
{
Messaging.SingleEmailMessage sendEmail = new Messaging.SingleEmailMessage();
sendEmail.setTemplateID('00XJ0000000Njzc');
sendEmail.setTargetObjectId(myNewContact.id);
sendEmail.setOrgWideEmailAddressId('0D2d0000000CbZv');
String[] CCEmails = new String[]{myNewContact.CC_Emails_To__c};
sendEmail.setCCAddresses(CCEmails);
emails.add(sendEmail);
Datetime textDateNew = myNewContact.Team_Training_Start__c;
String textDateStrNew = textDateNew.format('MM/dd/yyyy');
Datetime textDateOld = myOldContact.Team_Training_Start__c;
String textDateStrOld = textDateOld.format('MM/dd/yyyy');
Task InvTask = new Task();
InvTask.ActivityDate = date.today();
InvTask.WhoId = myNewContact.id;
InvTask.OwnerId = '005d0000001knHi';
InvTask.Priority = 'Normal';
InvTask.Status = 'Not Started';
InvTask.Subject = 'Please Read: Team Training Update sent to ' + myNewContact.FirstName + ' ' + myNewContact.LastName;
InvTask.Description = 'The Team Training date was changed for ' + myNewContact.FirstName + ' ' + myNewContact.LastName +' to ' + textDateStrNew + '.'
+ '\nAn email updating them of the new date has been sent.'
+ '\nPrevious Team Training Date: ' + textDateStrOld;
newTasks.add(InvTask);
}
}
}
if( newTasks!= null && newTasks.size() > 0)
{
insert newTasks ;
}
if (emails != NULL && emails.size() > 0)
{
Messaging.sendEmail(emails);
}
}