With the Winter '13 release of Salesforce, they have added native trigger support for Opportunity Teams (previously Selling Teams). We would like to have our users notified when they are added to a Opportunity Team. I have written the code below.
trigger EmailTeamMember2 on OpportunityTeamMember (after insert) {
Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();
for (OpportunityTeamMember o : trigger.new) {
String[] toAddresses = new String[] {o.User.email};
mail.setToAddresses(toAddresses);
mail.setReplyTo ('support@acme.com');
mail.setSenderDisplayname('Salesforce Support');
mail.setSubject('Added to an Opportunity Team : ');
mail.setUseSignature(false);
mail.setPlainTextBody('You have been added to the Opportunity Team of: ' + o.Opportunity);
mail.setHtmlBody('You have been added to the Opportunity Team of:<b> '+ o.Opportunity+');
Messaging.sendEmail(new Messaging.SingleEmailMessage[] { mail });
}
}
It saves without any errors, but when I try to add a team member this error appears:
Apex trigger EmailTeamMember2 caused an unexpected exception, contact your administrator: EmailTeamMember2: execution of AfterInsert caused by: System.EmailException: SendEmail failed. First exception on row 0; first error: INVALID_EMAIL_ADDRESS, Invalid to address : null: []: Trigger.EmailTeamMember2: line 16, column 1
Probably stating the obvious, but why not put in a system.debug statement and see what you are getting for email addresses?