0

In Apex How would I get from this:

    List<Account> accountsWithTeamMembers = new List<Account>(
    [select id FROM account where id IN (SELECT AccountId from AccountTeamMember]);

    List<AccountTeamMember> accountTeamMembers = new List<AccountTeamMember>(
[SELECT AccountId,TeamMemberRole, UserId, LastModifiedDate
 FROM AccountTeamMember WHERE AccountId=:accountsWithTeamMembers]);

To an iterable structure that gives me AccountID and "UserID corresponding to the latest entry in the Account's AccountTeamMembers for a given TeamMemberRole").

Ultimately, I need to asynchronously DML update a Lookup(User) field "Support_Representative__c" on Account using the value from the Account's most recently modified AccountTeamMember WHERE teammemberrole = 'Support Representative'.

I will be using scheduled apex to do the update for all Accounts, so really unless the solution can be generalized for all teammemberrole options (would be nice!) I plan to process this list of accounts:

    List<Account> accountsWithSupportRepTeamMembers = new List<Account>(
[select id from account where id IN 
(SELECT AccountId from AccountTeamMember 
WHERE teammemberrole = 'Support Representative']);

        List<AccountTeamMember> accountTeamMembers = new List<AccountTeamMember>(
[SELECT AccountId,TeamMemberRole, UserId, LastModifiedDate 
FROM AccountTeamMember WHERE AccountId=:accountsWithSupportRepTeamMembers]);

I think if we use Apex Maps to achieve this the key would be the concatenation of AccountID + UserId + TeamMemberRole. However I want to include only the latest (last modified) Account Team Member entry for the given TeamMemberRole)

Thanks for the guidance,

2
  • You want to have like a Map<ID, List<AccountTeamMember>>? Not sure what your asking
    – EricSSH
    Apr 30, 2018 at 20:59
  • That is accurate, i seek help creating a Map IdToLatestTeamMemberMap<AccountId, List<AccountTeamMember>> whereby AccountId is put into the map during iteration of accountsWithSupportRepTeamMembers, and whereby List<AccountTeamMember> contains up to 1 element (the latest (last modified) Account Team Member entry for the given TeamMemberRole) Apr 30, 2018 at 21:45

1 Answer 1

1

If you need to create a Map<ID, List<>> this will do it for you, I'm sure you can figure out the rest of your requirements once you get this far..

Map<ID, List<AccountTeamMember>> IdToLatestTeamMemberMap = new Map<ID, List<AccountTeamMember>>();

for(AccountTeamMember atm : [SELECT AccountId,TeamMemberRole, UserId, LastModifiedDate FROM AccountTeamMember WHERE AccountID =: accountsWithTeamMembers])
{
    if(IdToLatestTeamMemberMap.containsKey(atm.AccountID))
    {
        IdToLatestTeamMemberMap.get(atm.AccountID).add(atm);
    }
    else
    {
        IdToLatestTeamMemberMap.put(atm.AccountID, new List<AccountTeamMember>{atm});
    }
}

System.debug(IdToLatestTeamMemberMap);
2
  • There is no reason to do desc or asc, Maps do not maintain order of elements
    – EricSSH
    Apr 30, 2018 at 22:18
  • the problem is I dont want to .add(atm) because atm is a large list. During population of Accounts in the map I want their corresponding value to be just the latest entry for a given role. Eg for a given AccountID I want the Map to be TheAccountId, [Select UserId from AccountTeamMember WHERE AccountID =:TheAccountId ORDER BY LastModifiedDate desc LIMIT 1] but i dont want to run queries inside loops to thinking i need multiple maps and big queries for this solution. What do you think? Apr 30, 2018 at 22:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .