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
Kelsey ShannonKelsey Shannon 

Trigger Acting Incorrectly during Data Loader mass updates

Hello - I inherited an older organization and am a newbie developer. I would appreciate any advice you could give me!

We have a trigger that runs whenever a Opportunity Team Member is added or removed from an Opportunity and updates a custom field on the Opportunity populated with the name and employee ID of each team member. The end result could look something like this: "Shannon E334355, DeSanti E445466, Crosman E244562".

However, if we run data loader to insert or update team members with large batch sizes the trigger adds the names of team members for users on different opportunities. For example, if Opp A has User1 and User2 on the team, and Opp B had User3 and User4 on the team, after doing a bulk update that touches both Opps each will have the following text on the custom field: "User1, User2, User3, User4". 

Is there a best practice or way to prevent this from happening? Currently we are making sure to run in batches of 1 (which takes forever!).

Here is the actual code if that helps (and yes whoever wrote this added the code into the trigger rather than a separate class that the trigger calls...boo):
 
trigger OpportunityTeamMembers on Opportunity_Sales_Team__c (after delete,  after insert,after update) {

List<Id> oppList= new List<Id>();
List<Opportunity> oppListUpdate= new List<Opportunity>();
List<Opportunity_Sales_Team__c> oppTeamList= new List<Opportunity_Sales_Team__c>();
String teamName;
String tmName;

if (trigger.isInsert || trigger.isUpdate)
{
 for (Opportunity_Sales_Team__c oppTeamMember: Trigger.new)
    {
      if(trigger.isUpdate){
       //if(System.Trigger.oldMap.get(oppTeamMember.Id).User__c!=System.Trigger.NewMap.get(oppTeamMember.Id).User__c)
           if(oppTeamMember.User__c != null)
          oppList.add(oppTeamMember.Opportunity__c);
       }
      if(trigger.isInsert){
       if(oppTeamMember.User__c != NULL)
          oppList.add(oppTeamMember.Opportunity__c);
       }
   }   
}

if (Trigger.isDelete)
{  
    for (Opportunity_Sales_Team__c oppTeamMembers: Trigger.old)    {
          oppList.add(oppTeamMembers.Opportunity__c);  
           
    }
}   

if(oppList.size()>0){
   oppListUpdate=[Select Id,Programme_Manager__c from Opportunity where Id in :oppList];
   try{
   
   oppTeamList=[Select Id,User__r.User_EID__c,Team_Member_Full_Name__c,Opportunity__c, User__r.Name, User__c  from Opportunity_Sales_Team__c  where Opportunity__c in
                  :oppList order by createddate desc];
   }
   catch(Exception e){}              
   }
     try {
     for (integer i=0;i<oppListUpdate.size();i++){
     teamName='';
     tmName='';
        for(integer j=0;j<oppTeamList.size();j++){
        
          if(oppTeamList[j].User__c != NULL || oppTeamList[j].User__c != ''){
          if(oppTeamList[j].User__r.Name != NULL || oppTeamList[j].User__r.Name != ''){
              if(oppTeamList[j].User__r.Name !=null){
                  teamName = teamName + ',' + oppTeamList[j].User__r.Name.substring(oppTeamList[j].User__r.Name.lastIndexOf(' '))+' '+oppTeamList[j].User__r.User_EID__c;
              }
          }
          }
        }
        tmName = teamName.substring(teamName.indexOf(',')+1);

        oppListUpdate[i].TeamMemberLastName__c=tmName;
        
     
     }
     }catch(Exception ex){
        system.debug('Exception at 60:'+ex.getMessage());
     }
     try {
     if(oppListUpdate.size()>0)
          update oppListUpdate;
     }catch(Exception ex){
        system.debug('Exception at 66:'+ex.getMessage());
     }
}

 
Best Answer chosen by Kelsey Shannon
Steven NsubugaSteven Nsubuga
Hi Kelsey, I looked at your trigger and it seemed quite complex, so i took the liberty of simplifying it.
Below is my version of your trigger, I suggest you test it out in a sandbox first. Run the apex tests and do some dataloading to see if it fits the bill.  
 
trigger OpportunityTeamMembers on Opportunity_Sales_Team__c (after delete,  after insert,after update) {

	List<Id> oppList= new List<Id>();
	List<Opportunity> oppListUpdate= new List<Opportunity>();
	List<Opportunity_Sales_Team__c> oppTeamList= new List<Opportunity_Sales_Team__c>();
	String teamName;
	String tmName;

	if (trigger.isInsert || trigger.isUpdate){
		for (Opportunity_Sales_Team__c oppTeamMember: Trigger.new){
			oppList.add(oppTeamMember.Opportunity__c);
		}   
	}
	if (Trigger.isDelete){  
		for (Opportunity_Sales_Team__c oppTeamMembers: Trigger.old) {
			oppList.add(oppTeamMembers.Opportunity__c);  
		}
	}

	if(oppList.size()>0){
		oppListUpdate=[Select Id, TeamMemberLastName__c, Programme_Manager__c from Opportunity where Id in :oppList];
		
		oppTeamList=[Select Id,User__r.User_EID__c,Team_Member_Full_Name__c,Opportunity__c, User__r.Name, User__c  from Opportunity_Sales_Team__c  where Opportunity__c in
				  :oppList order by Opportunity__c];
		
		Map<String, List<Opportunity_Sales_Team__c>> oppTeamMemberMap = new Map<String, String>();
		
		for (Opportunity_Sales_Team__c oppTeamMember : oppTeamList) {
			List<Opportunity_Sales_Team__c> ost = oppTeamMemberMap.get(oppTeamMember.Opportunity__c);
			if (ost == null) {
				ost = new List<Opportunity_Sales_Team__c>();
			}
			ost.add(oppTeamMember);
			oppTeamMemberMap.put(oppTeamMember.Opportunity__c, ost);
		}		
		
		for (Opportunity opp: oppListUpdate){
			List<Opportunity_Sales_Team__c> salesTeam = oppTeamMemberMap.get(opp.Id);

			teamName = '';
            tmName='';

			for (Opportunity_Sales_Team__c st: salesTeam){
				if(st.User__c != NULL && st.User__c != '' && st.User__r.Name != NULL && st.User__r.Name != '') {
					teamName = teamName + ',' + st.User__r.Name.substring(st.User__r.Name.lastIndexOf(' '))+' '+st.User__r.User_EID__c;
				}
			}
			tmName = teamName.substring(1);
			opp.TeamMemberLastName__c=tmName;
		}
		update oppListUpdate;
	}
}
Fingers crossed  :)