function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Austin GutzAustin Gutz 

Opportunity Trigger that pulls Opportunity Team Member's email address

I've found a few posts that kind of address this need but I can't seem to get any of them to work.

I feel like I'm kind of close with this post: https://developer.salesforce.com/forums/?id=906F00000005KqHIAU

I need to trigger an email send from a custom object related to an Opportunity. The email needs to go to the Opportunity Team member with the role of Program Manager. I've added a custom email field on the Opportunity called PM_Email__c and had hoped I could update this field with process builder or a workflow rule... turns out that's not an option. I'd use this field to populate another email field on my custom object but I need to first find a way to get the email in the Opportunity field. Any help? 
Best Answer chosen by Austin Gutz
Abdul KhatriAbdul Khatri
Here is the trigger
 
trigger UpdateOppEmail on OpportunityTeamMember (after insert) {

	List<OpportunityTeamMember> memberList = new List<OpportunityTeamMember>();
    List<Opportunity> oppToUpdateList = new List<Opportunity>();
    List<Id> idUserList = new List<Id>();
    
    for(OpportunityTeamMember member : trigger.new) {
        
        if(member.TeamMemberRole != 'Program Manager') continue;
        
        memberList.add(member);
        idUserList.add(member.UserId);
    }
    
    if(memberList.isEmpty()) return;
    
    Map<Id, User> userMap = new Map<Id, User>([SELECT Id, Email FROM User WHERE Id IN :idUserList]);
    
    for(OpportunityTeamMember member : memberList) {
        
        Opportunity oppToUpdate = new Opportunity(Id = member.OpportunityId);
        oppToUpdate.PM_Email__c = userMap.get(member.UserId).Email;
        
        oppToUpdateList.add(oppToUpdate);
    }

    update oppToUpdateList;
}

 

All Answers

Abdul KhatriAbdul Khatri
Sorry for my being not getting it but if you can be little more clear on your expectation that would be awesome.
Austin GutzAustin Gutz
Absolutely! 

From an Opportunity - When someone is added as an Opportunity Team Member and his or her role is Program Manager, I need the email address for that user to be put into a custom field (PM_Email__c_) on the Opportunity. In our ORG we will only ever have 1 Opportunity Team member with that role on a single Opportunity at one time. 
Abdul KhatriAbdul Khatri
Here is the trigger
 
trigger UpdateOppEmail on OpportunityTeamMember (after insert) {

	List<OpportunityTeamMember> memberList = new List<OpportunityTeamMember>();
    List<Opportunity> oppToUpdateList = new List<Opportunity>();
    List<Id> idUserList = new List<Id>();
    
    for(OpportunityTeamMember member : trigger.new) {
        
        if(member.TeamMemberRole != 'Program Manager') continue;
        
        memberList.add(member);
        idUserList.add(member.UserId);
    }
    
    if(memberList.isEmpty()) return;
    
    Map<Id, User> userMap = new Map<Id, User>([SELECT Id, Email FROM User WHERE Id IN :idUserList]);
    
    for(OpportunityTeamMember member : memberList) {
        
        Opportunity oppToUpdate = new Opportunity(Id = member.OpportunityId);
        oppToUpdate.PM_Email__c = userMap.get(member.UserId).Email;
        
        oppToUpdateList.add(oppToUpdate);
    }

    update oppToUpdateList;
}

 
This was selected as the best answer
Austin GutzAustin Gutz
I get the following error:

Error: Compile Error: Incorrect SObject type: OpportunityTeamMember should be Opportunity at line -1 column -1

If I update the line to the Opportunity object then I get:

Error: Compile Error: Invalid loop variable type expected Opportunity was OpportunityTeamMember at line 7 column 5
Abdul KhatriAbdul Khatri
Can you check if the Opportunity Team is Enabled in your org

User-added image
 
Austin GutzAustin Gutz
I was trying place the trigger on the Opportunity object... my bad!

This seems to be working perfectly. Thank you!
Austin GutzAustin Gutz
Hey Abdul,

Any help on getting started with a test class for this trigger?