+ 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 receiving this error for the below:

"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"
 
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__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__c)) {
            survey.Current_AD_Test__c = accountToTeamMemberMap.get(survey.Account__c);
        }
    }
}

 
Best Answer chosen by Jemill
BalajiRanganathanBalajiRanganathan
you need to have the SOQL to include the accountId.

Select UserId, AccountId from AccountTeamMember..