+ Start a Discussion
Jeff BloomerJeff Bloomer 

Trigger for Opportunity Team Member

I have a very simple trigger I've written for adding two text fields to Opportunity Team Member that I then use in a flow.  It must be because it's the end of the day and I'm not thinking clearly, but I just need it to line up so that it doesn't mismatch the text values for the users with the actual records.  Right now, if I add more than 1 member to the Oppportunity Team, it enters the wrong person's name into Team Member Name and Team Member last name.  I just need to get them to match up as they're being entered.

 

trigger TRG_OpptyTeamMemberName on OpportunityTeamMember (before insert, before update) {

	List<User> teamMemberInfo = new List<User>();
	Set<ID> setTeamMemberID = new Set<ID>();
	Integer index = 0;
	
	for (OpportunityTeamMember otm: Trigger.new){
		setTeamMemberID.add(otm.UserId);
	}
	
	teamMemberInfo = [Select u.Name, u.LastName, u.Id From User u where u.Id in: setTeamMemberID];
	
	for (OpportunityTeamMember otm: Trigger.new){
		otm.Team_Member_Name__c = teamMemberInfo[index].Name;
		otm.Team_Member_LastName__c = teamMemberInfo[index].LastName;
		index +=1;
	}
	
}

Can someone please set me straight so that the values align as they go back into the record?

Best Answer chosen by Admin (Salesforce Developers) 
Naidu PothiniNaidu Pothini
trigger TRG_OpptyTeamMemberName on OpportunityTeamMember (before insert, before update)
{
	Set<Id> setTeamMemberID = new Set<Id>();
	
	for (OpportunityTeamMember otm: Trigger.new)
	{
		setTeamMemberID.add(otm.UserId);
	}

	Map<Id, User> usrMap = new Map<Id, User>([SELECT Id, Name, LastName FROM User WHERE Id IN :setTeamMemberId]);
	
	for (OpportunityTeamMember otm: Trigger.new)
	{
		User usr = usrMap.get(otm.UserId);

		otm.Team_Member_Name__c = usr.Name;
		otm.Team_Member_LastName__c = usr.LastName;
	}
}

 Try this...

 

 

 

trigger TRG_OpptyTeamMemberName on OpportunityTeamMember (before insert, before update) {

	List<User> teamMemberInfo = new List<User>();
	Set<ID> setTeamMemberID = new Set<ID>();
	Integer index = 0;
	
	for (OpportunityTeamMember otm: Trigger.new){
		setTeamMemberID.add(otm.UserId);  // 10 records with 5 userIDs then set will have 5 Ids
	}
	
	teamMemberInfo = [Select u.Name, u.LastName, u.Id From User u where u.Id in: setTeamMemberID]; // list will have 5 records
	
	for (OpportunityTeamMember otm: Trigger.new)
{ otm.Team_Member_Name__c = teamMemberInfo[index].Name; // index will go upto 10 but teammeberinfo will have only 5 records otm.Team_Member_LastName__c = teamMemberInfo[index].LastName; index +=1; } }

 

All Answers

Naidu PothiniNaidu Pothini
trigger TRG_OpptyTeamMemberName on OpportunityTeamMember (before insert, before update)
{
	Set<Id> setTeamMemberID = new Set<Id>();
	
	for (OpportunityTeamMember otm: Trigger.new)
	{
		setTeamMemberID.add(otm.UserId);
	}

	Map<Id, User> usrMap = new Map<Id, User>([SELECT Id, Name, LastName FROM User WHERE Id IN :setTeamMemberId]);
	
	for (OpportunityTeamMember otm: Trigger.new)
	{
		User usr = usrMap.get(otm.UserId);

		otm.Team_Member_Name__c = usr.Name;
		otm.Team_Member_LastName__c = usr.LastName;
	}
}

 Try this...

 

 

 

trigger TRG_OpptyTeamMemberName on OpportunityTeamMember (before insert, before update) {

	List<User> teamMemberInfo = new List<User>();
	Set<ID> setTeamMemberID = new Set<ID>();
	Integer index = 0;
	
	for (OpportunityTeamMember otm: Trigger.new){
		setTeamMemberID.add(otm.UserId);  // 10 records with 5 userIDs then set will have 5 Ids
	}
	
	teamMemberInfo = [Select u.Name, u.LastName, u.Id From User u where u.Id in: setTeamMemberID]; // list will have 5 records
	
	for (OpportunityTeamMember otm: Trigger.new)
{ otm.Team_Member_Name__c = teamMemberInfo[index].Name; // index will go upto 10 but teammeberinfo will have only 5 records otm.Team_Member_LastName__c = teamMemberInfo[index].LastName; index +=1; } }

 

This was selected as the best answer
Jeff BloomerJeff Bloomer

Much appreciated for the assist.  Exactly what I needed.  Thanks!

 

For anyone looking at the solution post, the one that works is the first code block out of the two.