+ Start a Discussion
Eric Blaxton.ax1862Eric Blaxton.ax1862 

Trigger help with Opportunity and Opportunity team members

Hi and thanks for reading this and for any help...

 

The requirement is to stop users from saving duplicate Team Member roles when they add or edit their Default Opportunity or Account Team.  

 

So, if Tom wants to add users to his default opp team, he can only add 1 "Sales Manager", 1 "Sales Lead", etc.

 

 

Eric

 

magicforce9magicforce9

Hi,

 

You can do this for OpportunityTeamMember via a trigger but not for AccountTeamMember object as its not supported for trigger.

 

Here is trigger code that does pretty much what you need...!

trigger OppTeamMemberTrigger on OpportunityTeamMember (before insert) {

	Set<id> oppIds = new Set<id>();
	for(OpportunityTeamMember member : trigger.new)
	{
		oppIds.add(member.OpportunityId)
 	}

	List<OpportunityTeamMember> teamMembers = [Select id, TeamMemberRole, OpportunityId from OpportunityTeamMember where OpportunityId IN : oppIds];
	Map<id, Set<String>> teamMembersMap = Map<id, List<String>>()

	//Organising Opportunity to team member roles in map to check for duplicates.
	for(OpportunityTeamMember member : teamMembers)
	{
		if(teamMembersMap.containsKey(member.OpportunityId))
			teamMembersMap.get(member.OpportunityId).add(member.TeamMemberRole);
		else
			teamMembersMap.put(member.OpportunityId, new Set<String> {member.TeamMemberRole} );

	}

	for(OpportunityTeamMember member : trigger.new){
		//Checking for duplicate teamMemberRole
		if(teamMemebersMap.get(member.OpportunityId).contains(member.TeamMemberRole))
		member.addError('There is already exist a ' + member.TeamMemberRole + ' in Opportunity Team, please remove him to add another person of same role.');

	}
}

 

Eric BlaxtonEric Blaxton
I was finally able to get to this project. Thank you very much...

I made some tweaks....had to deal with nullpointer exception by using try/catch, among other little things.


trigger OppTeamMemberTrigger on OpportunityTeamMember (before insert) {  
//This is for stopping a duplicate Opportunity Team Member from being inserted. 
  Set<id> oppIds = new Set<id>();
  for(OpportunityTeamMember member : trigger.new)
  {
    oppIds.add(member.OpportunityId);
   }// end for

  List<OpportunityTeamMember> teamMembers = [Select id, TeamMemberRole, OpportunityId from OpportunityTeamMember where OpportunityId IN : oppIds];
  Map<id, Set<String>> teamMembersMap = new Map<id, Set<String>>();
 
Try  
{
  //Organizing Opportunities and team member roles in map to check for duplicates.
  for(OpportunityTeamMember member : teamMembers)
  {
    if(teamMembersMap.containsKey(member.OpportunityId))
      teamMembersMap.get(member.OpportunityId).add(member.TeamMemberRole);
    else
      teamMembersMap.put(member.OpportunityId, new Set<String> {member.TeamMemberRole} );
  }// end for

  for(OpportunityTeamMember member : trigger.new){
    //Checking for duplicate teamMemberRole
    if(teamMembersMap.get(member.OpportunityId).contains(member.TeamMemberRole))
    member.addError(''A ' + member.TeamMemberRole + ' already exists on this Opportunity Team, please remove or edit existing persons role.');
  }// end for
}// end try 

catch (System.NullPointerException e)
{
// just temporary until i figure this out
System.debug('There was an error!');
}

}// end trigger
Will Jones 18Will Jones 18
Were you able to figure out how to check for duplicates upon initial creation? Also were you able to figure the nullpointer exception?
Eric BlaxtonEric Blaxton
Hi Will,

Yes, this worked fine on the Opportunity Team.  As far as I know, a trigger on the Account Team is not supported.  I dealt with the null pointer by adding the try/catch error handler.  Everything works fine.

Let me know if you need any clarification.

Thanks
shrey.tyagi88@tcs.comshrey.tyagi88@tcs.com
Hey Will,
      Put this piece of code between line 2 and line 3 (for initial creation duplicate check) . It worked for me. Hope it helps you!!!


for(Integer i=0;i<NewOpportunityTeamMembers.size();i++)
      {
        for (Integer j=i+1;j<NewOpportunityTeamMembers.size();j++)
        {
         // Check for dupliacte contract negotiators being added at the same time.
          if((NewOpportunityTeamMembers[i].OpportunityId==NewOpportunityTeamMembers[j].OpportunityId)&&
             (NewOpportunityTeamMembers[i].TeamMemberRole==NewOpportunityTeamMembers[j].TeamMemberRole)&&
             (NewOpportunityTeamMembers[i].TeamMemberRole=='Contract Negotiator'))
             {NewOpportunityTeamMembers[i].addError('Multiple people of Contract Negotiator role cannot be added to the same team. Please keep only one Contract Negotiator');}
       
        ​// Check for dupliacte Budget Finance Specialist's being added at the same time.
          if((NewOpportunityTeamMembers[i].OpportunityId==NewOpportunityTeamMembers[j].OpportunityId)&&
             (NewOpportunityTeamMembers[i].TeamMemberRole==NewOpportunityTeamMembers[j].TeamMemberRole)&&
             (NewOpportunityTeamMembers[i].TeamMemberRole=='Budget Finance Specialist'))
             {NewOpportunityTeamMembers[i].addError('Multiple people of Budget Finance Specialist role cannot be added to the same team. Please keep only one Budget Finance Specialist');}
        }
      }


Regards
Shrey Tyagi