3

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 : email1@test.com, email2@test.com: []: 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);
}


}

1 Answer 1

6

You have String[] CCEmails = new String[]{myNewContact.CC_Emails_To__c};

That creates List with a single String in it. In your case if the field CC_Emails_To__c has email1@test.com, email2@test.com as a value the result is a single List entry with the value of email1@test.com, email2@test.com which is not a valid email address.

You need to split the string before you put it into the List. Luckily there is a String.split instance method that you can use.

If your separator character is a comma you would do:

String[] CCEmails = myNewContact.CC_Emails_To__c.split(',');

That will result in a List with each enry containing just the email address. For example, when your field value is email1@test.com, email2@test.com you'll end up with a List with two entries.

1
  • @Jennifer No problem! Glad it worked for you. :-) Jan 11, 2014 at 17:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.