You need to sign in to do that
Don't have an account?
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;
}
You can try this code :
This should work for you.
Thanks
All Answers
You can try this code :
This should work for you.
Thanks
Thanks!