+ Start a Discussion
SolidLucasSolidLucas 

Trigger to update TeamMemberRole

Hello guys, i 've created  a field called vendedor__c on my opportunity and i want to create a trigger that compares if my  team member role is equal 'Sales Rep' if yes updates that field when my TeamMemberRole is equal 'Sales Rep' someone could help me with that?
Best Answer chosen by SolidLucas
Balaji BondarBalaji Bondar
Hi,
Use below trigger
trigger UpdateVendedorRole on OpportunityTeamMember (after insert, after update) {
Map <Id,Opportunity> opportunityMap = new Map <Id,Opportunity>();
List<Opportunity> opportunityObjList = new List<Opportunity>();
    for(OpportunityTeamMember opportunityTeamMemberObj : [select Id, TeamMemberRole, UserId, Opportunity.Id,Opportunity.vendedor__c from OpportunityTeamMember where Id IN:Trigger.New]){
        if(opportunityTeamMemberObj.TeamMemberRole == 'Sales Rep'){
            opportunity  opportunityObj = opportunityTeamMemberObj.Opportunity;
            opportunityObj.vendedor__c = opportunityTeamMemberObj.UserId;
            opportunityObjList.add(opportunityObj);
        }   
    }
    update opportunityObjList;
}
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.

All Answers

RamuRamu (Salesforce Developers) 
see if this works.
Trigger Update on OpportunityTeamMember(after insert, after update){
set<id> oppid=new set<id>();
	for(OpportunityTeamMember optm:trigger.new){
		if(TeamMemberRole=='Sales Rep'){
			oppid.add(optm.Opportunityid);
		}
	}
	
	list<opportunity> oppstoupdate= new list<opportunity>()
	list<opportunity> opps= new list<opportunity>([select vendedor__c,id from Opportunity where id:oppid]);
	for(Opportunity op:opps){
		op.vendedor__c=<value>;		
		oppstoupdate.add(op);
	}
	update oppstoupdate;
	
	
}

 
Balaji BondarBalaji Bondar
Hi,
Use below trigger
trigger UpdateVendedorRole on OpportunityTeamMember (after insert, after update) {
Map <Id,Opportunity> opportunityMap = new Map <Id,Opportunity>();
List<Opportunity> opportunityObjList = new List<Opportunity>();
    for(OpportunityTeamMember opportunityTeamMemberObj : [select Id, TeamMemberRole, UserId, Opportunity.Id,Opportunity.vendedor__c from OpportunityTeamMember where Id IN:Trigger.New]){
        if(opportunityTeamMemberObj.TeamMemberRole == 'Sales Rep'){
            opportunity  opportunityObj = opportunityTeamMemberObj.Opportunity;
            opportunityObj.vendedor__c = opportunityTeamMemberObj.UserId;
            opportunityObjList.add(opportunityObj);
        }   
    }
    update opportunityObjList;
}
Important :
If this is what you were looking for then please mark it as a "SOLUTION" or You can Click on the "Like" Button if this was beneficial for you.
This was selected as the best answer
SolidLucasSolidLucas

So guys i want that vendedor fields update when he is the Sales Rep, i just using the Select like bellow. But is not working.

:(
 

trigger MembroOportunidade_aiu on OpportunityTeamMember (after insert, after update) 
{
	//Declaração de variaveis
	
	for(OpportunityTeamMember om : Trigger.New){
		// verificar se papel do membro do time é igual a representante de vendas 	
		if( om.TeamMemberRole == ''){
			   Opportunity opp = [Select o.Vendedor__c, o.Id From Opportunity o];
			   
			// se papel correto, alterar vendedor da oportunidade	
			
			update opp;
		}
	}		
}