+ Start a Discussion
cambart14cambart14 

Opportunity Team Notification

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  

 

Thanks for your help in advance!!! :)

Best Answer chosen by Admin (Salesforce Developers) 
cambart14cambart14

I was able to get the code working below for our instance.  It does a very simple email, but it works for our purposes. 

 

trigger EmailTeamMember2 on OpportunityTeamMember (after insert) {

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

    for (OpportunityTeamMember o : trigger.new) { 
    String[] showID = new String[] {o.UserID};
    List<User> u = [SELECT Id, Name, Email FROM User WHERE Id in :showID];
    String[] toAddresses = new String[] {u[0].email};
    String[] oppID = new String[] {o.OpportunityID};
    

    mail.setToAddresses(toAddresses);
    mail.setReplyTo ('noreply@salesforce.com');
    mail.setSenderDisplayname('Salesforce Support');
    mail.setSubject('Added to an Opportunity Team ');
    mail.setUseSignature(false);
    mail.setPlainTextBody('You have been added to an Opportunity Team ');
    mail.setHtmlBody('You have been added to an Opportunity Team:<b><p><p/> '+ 
     'To view that opportunity <a href=https://na3.salesforce.com/'+o.OpportunityID+'>click here.</a>');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    
    }
}

 

All Answers

Scott-3823Scott-3823

Hi cambart, did you figure this out? I'm looking to do the same thing. Thanks!

cambart14cambart14

I was able to get the code working below for our instance.  It does a very simple email, but it works for our purposes. 

 

trigger EmailTeamMember2 on OpportunityTeamMember (after insert) {

    Messaging.SingleEmailMessage mail = new Messaging.SingleEmailMessage();

    for (OpportunityTeamMember o : trigger.new) { 
    String[] showID = new String[] {o.UserID};
    List<User> u = [SELECT Id, Name, Email FROM User WHERE Id in :showID];
    String[] toAddresses = new String[] {u[0].email};
    String[] oppID = new String[] {o.OpportunityID};
    

    mail.setToAddresses(toAddresses);
    mail.setReplyTo ('noreply@salesforce.com');
    mail.setSenderDisplayname('Salesforce Support');
    mail.setSubject('Added to an Opportunity Team ');
    mail.setUseSignature(false);
    mail.setPlainTextBody('You have been added to an Opportunity Team ');
    mail.setHtmlBody('You have been added to an Opportunity Team:<b><p><p/> '+ 
     'To view that opportunity <a href=https://na3.salesforce.com/'+o.OpportunityID+'>click here.</a>');
    Messaging.sendEmail(new Messaging.SingleEmailMessage[] {mail});
    
    }
}

 

This was selected as the best answer
Prasad Suravkar 1Prasad Suravkar 1
Hi,Can anyone help me out in this trigger if i want to display opportunity name,owner name in the mail body?