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
jjvdevjjvdev 

OpportunityTeamMember Trigger

Is it possible to write a trigger on OpportunityTeamMember object?

 

-Thanks

DharmeshDharmesh

OpportunityTeamMember doesn't allow trigger on it.

jjvdevjjvdev

Too bad, is there a way to trigger off OpportunityTeamMember data through the User object to update a related Opportunity?

 

-Thanks

 

I created this trigger but it only updates the Opportunity when the related OpportunityTeamMember User record is updated, not the OpportunityTeamMember record itself.

 

 

//This trigger updates the opportunity field HasTeam__c
//When an Opportunity has a Sales Team member(s) update the opportunity field HasTeam to TRUE

trigger OpportunityHasTeam on User (before insert, before update) {

//Create a collection of Users
Set<Id> uIds = new Set<Id>();
for (User u: trigger.new) {
String uId = u.Id;
if(uId!=null && !uIds.contains(uId)){
uIds.add(uId);
}
}

//Create a list of OpportunityTeamMembers related to the Users im the trigger
List<OpportunityTeamMember> oppTeam = [Select UserId, OpportunityId From OpportunityTeamMember Where UserId IN:uIds];

//Create a collection of Opportunity Ids that are related to the Users in the trigger
Set<Id> oppId = new Set<Id>();
for (OpportunityTeamMember team: oppTeam) {
String oId = team.OpportunityId;
if(oId!=null && !oppId.contains(oId)){
oppId.add(oId);
}
}

//Pull in related OpportunityTeamMembers Ids
Map<Id,OpportunityTeamMember> teamMap = new Map<Id,OpportunityTeamMember>();
for(OpportunityTeamMember otm: oppTeam){
teamMap.put(otm.OpportunityId,otm);
}

//Map of related opportunities to be updated
Map<Id,Opportunity> OpportunitiesToBeUpdated = new Map<Id,Opportunity>();

//Update the Has_Team__c field
List<Opportunity> oppList = [Select Id From Opportunity Where Id IN:oppId];
for(Opportunity o: oppList) {
String oId = o.Id;
if(oId!=null) {
OpportunityTeamMember thisOpportunityTeamMember = teamMap.get(o.Id);
if(thisOpportunityTeamMember!=null) {
o.HasTeam__c=TRUE;
} else
if(thisOpportunityTeamMember==null) {
o.HasTeam__c=FALSE;
}
}
OpportunitiesToBeUpdated.put(o.Id,o);
}

if(OpportunitiesToBeUpdated.size()>0) {
update OpportunitiesToBeUpdated.values();
}

}