+ Start a Discussion
JemillJemill 

Write Account Team Member to Custom Object lookup field

Hi,

    I'm trying to create a trigger on my Custom Object named Survey that places an Account Team Member of role = CJ Advertiser Account Director into my lookup field Current AD Test.  I am new to coding and am aware my code might be extremely wrong.  Any help appreciated!
trigger AccountDirector on Survey__c (before update, after insert) {
    for (Survey_c SUR : Trigger.net){
        try{
            ID samID = [select UserId from AccountTeamMember where AccountId = : SUR.Account_link_id and TeamMemberRole = 'CJ Advertiser Account Director'].UserId; SUR.Current_AD_Test_c = samID;
            }
      }
}

 
Best Answer chosen by Jemill
pconpcon
What you will want to do is to build up a list of all the accounts to query, fetch the team members and then apply them to your survey.  I changed this to trigger before in both cases.  You may need to update the Account_Link_Id__c and Current_Ad_Test__c fields if that is not their API name
 
trigger AccountDirector on Survey__c (before update, before insert) {
    Set<Id> accountIds = new Set<Id>();

    for (Survey__c survey: Trigger.new) {
        accountIds.add(survey.Account_Link_Id__c;
    }

    accountIds.remove(null);

    Map<Id, Id> accountToTeamMemberMap = new Map<Id, Id>();

    for (AccountTeamMember teamMember: [
        select UserId
        from AccountTeamMember
        where AccountId in :accountIds and
            TeamMemberRole = 'CJ Advertiser Account Director'
    ]) {
        accountToTeamMemberMap.put(teamMember.AccountId, teamMember.UserId);
    }  

    for (Survey__c survey: trigger.new) {
        if (accountToTeamMemberMap.containsKey(survey.Account_Link_Id__c)) {
            survey.Current_Ad_Test__c = accountToTeamMemberMap.get(survey.Account_Link_Id__c);
        }
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors.

All Answers

pconpcon
What you will want to do is to build up a list of all the accounts to query, fetch the team members and then apply them to your survey.  I changed this to trigger before in both cases.  You may need to update the Account_Link_Id__c and Current_Ad_Test__c fields if that is not their API name
 
trigger AccountDirector on Survey__c (before update, before insert) {
    Set<Id> accountIds = new Set<Id>();

    for (Survey__c survey: Trigger.new) {
        accountIds.add(survey.Account_Link_Id__c;
    }

    accountIds.remove(null);

    Map<Id, Id> accountToTeamMemberMap = new Map<Id, Id>();

    for (AccountTeamMember teamMember: [
        select UserId
        from AccountTeamMember
        where AccountId in :accountIds and
            TeamMemberRole = 'CJ Advertiser Account Director'
    ]) {
        accountToTeamMemberMap.put(teamMember.AccountId, teamMember.UserId);
    }  

    for (Survey__c survey: trigger.new) {
        if (accountToTeamMemberMap.containsKey(survey.Account_Link_Id__c)) {
            survey.Current_Ad_Test__c = accountToTeamMemberMap.get(survey.Account_Link_Id__c);
        }
    }
}

NOTE: This code has not been tested and may contain typographical or logical errors.
This was selected as the best answer
JemillJemill
Thanks so much pcon!  I was able to save, but am getting the error 
"Error: Invalid Data. 
Review all error messages below to correct your data.
Apex trigger AccountDirector caused an unexpected exception, contact your administrator: AccountDirector: execution of BeforeInsert caused by: System.SObjectException: SObject row was retrieved via SOQL without querying the requested field: AccountTeamMember.AccountId: Trigger.AccountDirector: line 18, column 1"
pconpcon
Sorry, just add accountId to the AccountTeam soql query
JemillJemill
Worked! Thanks so much!  Any suggestions for a begginers book for triggers?