+ Start a Discussion
KCrock88KCrock88 

Trigger not pulling Account Team members into custom object

Hello - hope someone can help out. I have a custom object, "Services", that tracks customer onboarding post sale. 

So when the Opportunity is marked closed won, a trigger will automatically insert a "Services" record for that account - pulling in information from the related Opportunity and Account (Both account and related opportunity fields are custom lookup fields on the Service object, NOT master detail).

 

What I am trying to do is pull two members from the Account Team of that Account into the Service:

1.) The user marked as the "Integration Manager" on the Account Team will be the Service Owner

2.) The user marked as the "Client Manager" on the Account Team will be pulled into a custom user lookupfield "Client Manager" on the service record

 

Below is my trigger, attempting to pull from AccountTeamMember

         for(AccountTeamMember teamMember:[select id,TeamMemberRole from AccountTeamMember where accountId=:OPP.AccountID]){
               
                if (teamMember.TeamMemberRole=='Integration Manager'){
                    SVC.OwnerId = teammember.id;
                } else if (teamMember.TeamMemberRole=='Client Manager'){
                    SVC.Client_Manager__c = teammember.id;
                }  
                
            }

 

However, I am getting the following error upon save: Error:Apex trigger ServiceCreation caused an unexpected exception, contact your administrator: ServiceCreation: execution of AfterUpdate caused by: System.DmlException: Insert failed. First exception on row 0; first error: FIELD_INTEGRITY_EXCEPTION, Client Manager: id value of incorrect type: 01Mn0000000DVyNEAW: [Client_Manager__c]: Trigger.ServiceCreation: line 51, column 1

 

 

I think I am pulling the ID of the teammember role, rather than the user itself. Looking through workbench, I can not find the right value to set these users. Can anybody help?

 

Best Answer chosen by Admin (Salesforce Developers) 
KCrock88KCrock88

Nevermind...was able to figure it out.

for(AccountTeamMember teamMember:[select UserId,TeamMemberRole from AccountTeamMember where accountId=:OPP.AccountID]){
               
                if (teamMember.TeamMemberRole=='Integration Manager'){
                    SVC.OwnerId = teammember.UserId;
                } else if (teamMember.TeamMemberRole=='Client Manager'){
                    SVC.Client_Manager__c = teammember.UserId;
                }  
                
            }

 

All Answers

KCrock88KCrock88

Nevermind...was able to figure it out.

for(AccountTeamMember teamMember:[select UserId,TeamMemberRole from AccountTeamMember where accountId=:OPP.AccountID]){
               
                if (teamMember.TeamMemberRole=='Integration Manager'){
                    SVC.OwnerId = teammember.UserId;
                } else if (teamMember.TeamMemberRole=='Client Manager'){
                    SVC.Client_Manager__c = teammember.UserId;
                }  
                
            }

 

This was selected as the best answer
JemillJemill
Hi KCrock88,

         I'm trying to add the Account Team member of role = CJ Advertiser Account Director to my custom object Survey in my lookup field Current_AD_Test_c.  I am new to coding and this was the closes example of what I was trying to accomplish with my trigger.  Is there anyway to look at my code and see where I am going wrong (probably everywhere).  Thanks!
 
trigger SurveyUpdates on Survey (after insert) {
    for(AccountTeamMember teamMember:[select <strong><font color="#FF0000">UserId</font></strong>,TeamMemberRole from AccountTeamMember where accountId=:SUR.AccountID]){
 
                 if (teamMember.TeamMemberRole=='CJ Advertiser Account Director'){
                     Survey.Current_AD_Test__c = teammember.<strong><font color="#FF0000">UserId</font></strong>;
                 } 

            }
}

 
KCrock88KCrock88
Hi Jake, I am not really a developer either, but your trigger has an IF built into with, without an ELSE IF.  I am also not sure what the rest of your trigger looks like, or what errors you are receiving. You are off to the right start, I copied in the code that worked for me below (this is only a portion of my trigger). I would take that code and start a new thread on this site for someone to chime in. 
Services__c SVC = new Services__c();
            //Queries Opportunity Team Member Table, returns list of User IDs and Team Member Roles
            for(OpportunityTeamMember teamMember:[select UserID,TeamMemberRole from OpportunityTeamMember where OpportunityId=:OPP.ID]){
               //Sets Service Owner and Client Manager in Service record based on Opportunity Team Members
                if (teamMember.TeamMemberRole=='Activation Manager'){
                    SVC.OwnerId = teammember.userId;
                } else if (teamMember.TeamMemberRole=='Client Manager'){
                    SVC.Client_Manager__c = teammember.userId;
                }  
                
            }

 
JemillJemill
Thanks!