+ Start a Discussion
ReeceReece 

Opportunity Team Member Help

I am trying to loop through a list of team members on an opportunity and am having troubles. Here's the piece that isn't working how I need it to:

 

        for (Opportunity opp : tNewMap.values()) {

 

//This code section works, but obviously really bad for bulk loads

                List<OpportunityTeamMember> oppMembers = [SELECT UserId FROM OpportunityTeamMember WHERE OpportunityId =: opp.Id];      
                for (OpportunityTeamMember otm : oppMembers) {

 

 

//This doesn't work and I'm not sure why
                for (OpportunityTeamMember otm : opp.OpportunityTeamMembers) {

 

 

Any advice?

DharmeshDharmesh

You can do something like this

 

Trigger on Opportunity(before update){

 

set<id> ids = Trigger.newMap.keyset();

List<OpportunityTeamMember> oppMembers = [SELECT UserId FROM OpportunityTeamMember WHERE OpportunityId in :ids];  

for (OpportunityTeamMember otm : oppMembers) {

//your logic

}

}

jhurstjhurst

Reece,

 

 

for (OpportunityTeamMember otm : opp.OpportunityTeamMembers) {

 

 

This is not working likely because of what data is inside of your "opp" variable.  Likely you are pulling trigger.NewMap for this value.  This is a map of IDs (Opportunity in this case) and the SOBject associated with the ID.  Unfortunately, it will not contain a list of all child data associated with that SObject.  It will only contain the data that is modified in the new version of the Opportunity.

 

What you will want to do (to avoid the query inside the for loop) would be to build your own map of OpportunityTeamMembers to OpportunityID:

 

 

Map<ID, List<OpportunityTeamMember>> otmMap = new Map<ID, List<OpportunityTeamMember>>();
for(OpportunityTeamMember otm : [SELECT Id, OpportunityAccessLevel, OpportunityId, TeamMemberRole, UserId FROM OpportunityTeamMember where OpportunityID in :Trigger.NewMap.keySet()]) {
     List<OpportunityTeamMember> otmList = new List<OpportunityTeamMember>();
     if(otmMap.containsKey(otm.OpportunityId)) {
         otmList = otmMap.get(otm.OpportunityId).deepClone();
     }
     otmList.add(otm);
     otmMap.put(otm.OpportunityId, otmList);
}
for (Opportunity opp : Trigger.NewMap.values()) {
     for (OpportunityTeamMember otm2 : otmMap.get(opp.Id)) {
          ...
     }
     ...
}

 

 

Hope this helps

Jay

ReeceReece

Thanks all! I took the query ouside of the loop and everything seems to be working now. Duh!

SwitchSwitch

Hello,

 

I'm trying to create a trigger to validate an opportunity (must have at least one opportunity team member (we're doing away with our custom sales rep fields to accomodate an SFDC-DSM integration).  I am new to apex and I faintly grasp Jay's mapping suggestion.  Initially, I wanted to just get a list of opportunity team members for all new opportunities, and then require the user to add a team member after the opp has just been created (or before, if possible, but this doesn't seem to flow with the UI).  If the list is empty, then the user should be required to enter an opportunity team member.

 

Can someone point out my mistakes with the code below?

 

I'm getting this error: "Compile Error: Method does not exist or incorrect signature: otmList.isEmpty() at line 14 column 12".

 

 

trigger SalesTeamValidation on Opportunity (after insert, after update) {

Map<ID, List<OpportunityTeamMember>> otmMap = new Map<ID, List<OpportunityTeamMember>>();
for(OpportunityTeamMember otm : [SELECT Id, OpportunityAccessLevel, OpportunityId, TeamMemberRole, UserId FROM OpportunityTeamMember where OpportunityID in :Trigger.NewMap.keySet()]) {
List<OpportunityTeamMember> otmList = new List<OpportunityTeamMember>();
if(otmMap.containsKey(otm.OpportunityId)) {
otmList = otmMap.get(otm.OpportunityId).deepClone();
}
otmList.add(otm);
otmMap.put(otm.OpportunityId, otmList);
}
for (Opportunity opp : Trigger.NewMap.values()) {
for (OpportunityTeamMember otm2 : otmMap.get(opp.Id)) {
if(otmList.isEmpty()==true){  //<-- this is the line that throws the error
System.debug('You must designate a Sales Rep in the Opportunity Team Section'); // Is this the correct way of sending an error message to the UI?
}
}
}

}//end trigger

 

Thanks in advance,

 

Manny