2

I built out a trigger that upon an opportunity reaching a certain stage will query the Account Team members of the related account and insert those records matching a certain criteria as Opportunity Team Members.

My question revolves around using the map. Currently, I am just taking the first Relationship Manager and Technical Account Manager. The code works in the case where those are listed but it breaks if one of those aren't listed and i receive the error

"CopyActTeam: execution of AfterUpdate caused by: System.NullPointerException: Attempt to de-reference a null object Trigger.CopyActTeam: line 30, column 1: "

What is a better approach if I want to grab all of the Relationship Managers on the account? Thank you

   trigger CopyActTeam on Opportunity (after update) {

    set<ID> AccountIDs = new set<ID>();
    map<ID,AccountTeamMember> RMAcctTeamMap = new Map<ID,AccountTeamMember>();
    map<ID,AccountTeamMember> TAMAcctTeamMap = new Map<ID,AccountTeamMember>();
    List<OpportunityTeamMember> oppTeamMembersList = new List<OpportunityTeamMember>();

    for(Opportunity o : Trigger.new){
        if(o.StageName == 'Stage 5'){
        AccountIDs.add(o.accountid);
        }
    }

    for(AccountTeamMember atm : [Select a.UserId,a.User.Name,a.TeamMemberRole, a.Id, a.AccountId 
                            From AccountTeamMember a 
                            where a.TeamMemberRole IN ('Relationship Manager','Technical Account Manager') AND a.AccountId in :AccountIDs]){

        if(atm.TeamMemberRole == 'Relationship Manager') {
        RMAcctTeamMap.put(atm.Accountid,atm);
        }
        if(atm.TeamMemberRole == 'Technical Account Manager') {
            TAMAcctTeamMap.put(atm.Accountid,atm);
        }

    }

    for(Opportunity o : Trigger.new){
        if(o.StageName == 'Stage 5'){
            OpportunityTeamMember rm = new OpportunityTeamMember();
            rm.UserId = RMAcctTeamMap.get(o.AccountId).UserId;
            rm.TeamMemberRole = 'Relationship Manager';
            rm.OpportunityId = o.ID;
            oppTeamMembersList.add(rm);
         }
         if(o.StageName == 'Stage 5'){
            OpportunityTeamMember tm = new OpportunityTeamMember();
            tm.UserId = TAMAcctTeamMap.get(o.AccountId).Userid;
            tm.TeamMemberRole = 'Technical Account Manager';
            tm.OpportunityId = o.ID;
            oppTeamMembersList.add(tm); 
         }
    }  
    insert oppTeamMembersList; 
}
  • @Coffee everyone is happy to help you troubleshoot specific issues, but general "make my code work" requests aren't going to get much attention. Could you instead post a specific issue you're having trouble with? i.e. I'm creating stuff here, I'd expect this to happen, but instead this happens. Once that issue is resolved create as many new questions as you want to address the next issue you run into. – Ralph Callaway Dec 18 '13 at 1:36
  • Thanks for the feedback Ralph, I appreciate it. That's good advice and it was definitely not my intention. I will update my post accordingly! – Coffee Dec 18 '13 at 3:01
2

If I understand your requirement correctly:

For every Opportunity that reaches Stage 5, copy certain Account Team Members across into Opportunity Team Members.

The following code fulfils your requirement

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

Map< Id , Id > opportunityIdToAccountId = new Map< Id , Id >();
for(Opportunity o : Trigger.new)  {
    if(o.StageName == 'Stage 5')   {
        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
                        WHERE a.TeamMemberRole IN ('Relationship Manager','Technical Account Manager') AND 
                        a.AccountId in :opportunityIdToAccountId.values() ])   {

        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(o.StageName == 'Stage 5')   {
        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;
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.