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
sml9099sml9099 

Apex trigger not working with "Add default Team " button on Opportunity

Hey, 
SO, I have an Apex trigger on opportunity Team Member which copies the team member with user role = ' Campaign Manager' to "Active_Campaign_Manager__c" field at opportunity. This trigger works fine when I individually add team members but it does NOT work when I click "ADD DEFAULT TEAM". It just does not fire . Below is my code. Please take a look and guide me.

trigger InsertOppTeam2ACM on OpportunityTeamMember (after insert, after update) {
//Using set to be able to work with setOpp and setOTM SETS

    set<Id> setOpp = new set<Id>();

    set<Id> setOTM = new set<Id>();
    

        for (OpportunityTeamMember oppTeam : trigger.new) {
         
          setOpp.add(oppTeam.OpportunityId);
          setOTM.add(oppTeam.id);
          }
//Loop through opportunity team members and grab Users who have these roles 'Campaign Manager'.

    list<OpportunityTeamMember> listOTM  = new list<OpportunityTeamMember>    
        ([
            SELECT Id, UserId, OpportunityId,  TeamMemberRole, User.Name FROM OpportunityTeamMember WHERE Id IN :setOTM AND
            (TeamMemberRole ='Campaign Manager') ]);

                                                   
// Create a map that grabs the Opportunity Id being worked with
      Map<Id,Opportunity> mapOpps = new map<Id, Opportunity>([SELECT Id FROM Opportunity Where Id = :setOpp ]) ;
 
        Opportunity tempOpp;

  
//Load the ID's
            for(OpportunityTeamMember otm : listOTM ){
                tempOpp = mapOpps.get(otm.OpportunityId);
        
            if(otm.TeamMemberRole == 'Campaign Manager') {
                tempOpp.Active_Campaign_Manager__c =  otm.User.Name;
              }


         }     
 
// Load values
 
update mapOpps.values();
}
kevin lamkevin lam
It's as designed according to this knowledge article:
http://help.salesforce.com/HTViewSolution?id=000187820&language=en_US
The Real Scott MorrisonThe Real Scott Morrison
It may be as designed, but let me just state the obvious - it is a horrible design decision, let's call it a design bug shall we.
Shiva RajendranShiva Rajendran
Hi smi9099,
I have refined a bit of your code.
trigger InsertOppTeam2ACM on OpportunityTeamMember (after insert, after update) {
//Using set to be able to work with setOpp and setOTM SETS

    set<Id> setOpp = new set<Id>();

    set<Id> setOTM = new set<Id>();
    

        for (OpportunityTeamMember oppTeam : trigger.new) {
        if(oppTeam.TeamMemberRole =='Campaign Manager')
             {      
               setOTM.add(oppTeam.id);
                 setOpp.add(oppTeam.OpportunityId);
              }
          }
//Loop through opportunity team members and grab Users who have these roles 'Campaign Manager'.

    list<OpportunityTeamMember> listOTM  = new list<OpportunityTeamMember>    
        ([
            SELECT Id, UserId, OpportunityId,  TeamMemberRole, User.Name FROM OpportunityTeamMember WHERE Id IN :setOTM  ]); 
//  im not sure if you want this step,this could be shrinken

                                                   
// Create a map that grabs the Opportunity Id being worked with
      Map<Id,Opportunity> mapOpps = new map<Id, Opportunity>([SELECT Id FROM Opportunity Where Id In :setOpp ]) ;
 
        Opportunity tempOpp;

  
//Load the ID's
            for(OpportunityTeamMember otm : listOTM ){
                tempOpp = mapOpps.get(otm.OpportunityId);
        
          
                tempOpp.Active_Campaign_Manager__c =  otm.User.Name;
            


         }     
 
// Load values
 
update mapOpps.values();
}
By the way your code should work, could you share the details about ADD DEFAULT TEAM functionality ,what does it do, does it add array of OpportunityTeamMember ?
Try to insert some list of opportunityTeamMember in anonymous and check if the trigger is getting called or not.
Thanks and Regards,
Shiva RV