+ Start a Discussion
Eric Chan 45Eric Chan 45 

Trigger Dereferencing Null pointer exception

Hello! 

I have a trigger that copies AccountTeam to OpportunityTeam After Insert

However, im getting a referencing null exception error when there are no AccountTeamMembers to copy

 

How do i change this to only run if there are AccountTeamMembers to copy over?



 

trigger AcctTeamtoOpp on Opportunity (after insert) {

    List<OpportunityTeamMember> oppTeamMembersList = new List<OpportunityTeamMember>();

Map< Id , Id > opportunityIdToAccountId = new Map< Id , Id >();
for(Opportunity o : Trigger.new)  {
   
        opportunityIdToAccountId.put(o.Id,  o.AccountId );
  
}


Map<id, List<AccountTeamMember > > accountIdToAccountTeamMembers = new    Map<id,  List<AccountTeamMember > > ();
for(AccountTeamMember accountTeamMember : [SELECT a.UserId,a.User.Name,a.TeamMemberRole, a.Id, a.AccountId
                        FROM AccountTeamMember a])   {

        List<AccountTeamMember > accountTeamMembers = ( accountIdToAccountTeamMembers.get(accountTeamMember.Accountid) == null) ?
                                                       new  List<AccountTeamMember >() :
                                                       accountIdToAccountTeamMembers.get(accountTeamMember.Accountid);

        accountTeamMembers.add(accountTeamMember);
        accountIdToAccountTeamMembers.put(accountTeamMember.Accountid, accountTeamMembers);

}



for(Opportunity o : Trigger.new) {
    
        Id accountId  = opportunityIdToAccountId.get(o.Id);
        for ( AccountTeamMember accountTeamMember : accountIdToAccountTeamMembers.get(accountId) )  {
            OpportunityTeamMember opportunityTeamMember  = new OpportunityTeamMember();
            opportunityTeamMember.UserId = accountTeamMember.UserId;
            opportunityTeamMember.TeamMemberRole = accountTeamMember.TeamMemberRole;
            opportunityTeamMember.OpportunityId = o.ID;
            oppTeamMembersList.add(opportunityTeamMember);
       
     }
}

insert oppTeamMembersList;
    
    
}

Best Answer chosen by Eric Chan 45
Vikash GoyalVikash Goyal
Hi Eric,

You can try this code :
trigger AcctTeamtoOpp on Opportunity (after insert) {
    
    List<OpportunityTeamMember> oppTeamMembersList = new List<OpportunityTeamMember>();
    Set<Id> accountIds = new Set<Id>();
    for(Opportunity o : Trigger.new)  {
        accountIds.add(o.AccountId);
    }
    
    Map<Id, List<AccountTeamMember>> accountIdToAccountTeamMembers = new Map<Id, List<AccountTeamMember>> ();
    
    for(AccountTeamMember accountTeamMember : [SELECT UserId,User.Name, TeamMemberRole, Id, AccountId 
                                               FROM AccountTeamMember 
                                               WHERE AccountId IN : accountIds]){
        
        List<AccountTeamMember> accountTeamMembers = (accountIdToAccountTeamMembers.get(accountTeamMember.Accountid) == null) 
            ? new List<AccountTeamMember>() : accountIdToAccountTeamMembers.get(accountTeamMember.Accountid);
        
        accountTeamMembers.add(accountTeamMember);
        accountIdToAccountTeamMembers.put(accountTeamMember.Accountid, accountTeamMembers);
    }
    
    for(Opportunity o : Trigger.new) {
        if(accountIdToAccountTeamMembers.containsKey(o.AccountId)){
            for (AccountTeamMember accountTeamMember : accountIdToAccountTeamMembers.get(o.AccountId) )  {
                OpportunityTeamMember opportunityTeamMember  = new OpportunityTeamMember();
                opportunityTeamMember.UserId = accountTeamMember.UserId;
                opportunityTeamMember.TeamMemberRole = accountTeamMember.TeamMemberRole;
                opportunityTeamMember.OpportunityId = o.ID;
                oppTeamMembersList.add(opportunityTeamMember);
            }
        }
    }
    
    if(oppTeamMembersList.size() > 0){
    	insert oppTeamMembersList;
    }
}

This should work for you.

Thanks

All Answers

Vikash GoyalVikash Goyal
Hi Eric,

You can try this code :
trigger AcctTeamtoOpp on Opportunity (after insert) {
    
    List<OpportunityTeamMember> oppTeamMembersList = new List<OpportunityTeamMember>();
    Set<Id> accountIds = new Set<Id>();
    for(Opportunity o : Trigger.new)  {
        accountIds.add(o.AccountId);
    }
    
    Map<Id, List<AccountTeamMember>> accountIdToAccountTeamMembers = new Map<Id, List<AccountTeamMember>> ();
    
    for(AccountTeamMember accountTeamMember : [SELECT UserId,User.Name, TeamMemberRole, Id, AccountId 
                                               FROM AccountTeamMember 
                                               WHERE AccountId IN : accountIds]){
        
        List<AccountTeamMember> accountTeamMembers = (accountIdToAccountTeamMembers.get(accountTeamMember.Accountid) == null) 
            ? new List<AccountTeamMember>() : accountIdToAccountTeamMembers.get(accountTeamMember.Accountid);
        
        accountTeamMembers.add(accountTeamMember);
        accountIdToAccountTeamMembers.put(accountTeamMember.Accountid, accountTeamMembers);
    }
    
    for(Opportunity o : Trigger.new) {
        if(accountIdToAccountTeamMembers.containsKey(o.AccountId)){
            for (AccountTeamMember accountTeamMember : accountIdToAccountTeamMembers.get(o.AccountId) )  {
                OpportunityTeamMember opportunityTeamMember  = new OpportunityTeamMember();
                opportunityTeamMember.UserId = accountTeamMember.UserId;
                opportunityTeamMember.TeamMemberRole = accountTeamMember.TeamMemberRole;
                opportunityTeamMember.OpportunityId = o.ID;
                oppTeamMembersList.add(opportunityTeamMember);
            }
        }
    }
    
    if(oppTeamMembersList.size() > 0){
    	insert oppTeamMembersList;
    }
}

This should work for you.

Thanks
This was selected as the best answer
Eric Chan 45Eric Chan 45
Thank you! works so far! going to deploy!

Thanks!