+ Start a Discussion
Nirav_ShahNirav_Shah 

Add TeamMemberRole from List to Set

Hi Folks,

Following is my code
List <AccountTeamMember> teamMemberList = new List <AccountTeamMember>();
            
            Set <String> teamMemberRole = new Set <String>();
                
                if(accountDetail.Ultimate_Parent__c && accountDetail.Parent__c){
                    teamMemberList = createTeamMemberList(accountDetail.Id, teamMemberRole);
                }
                else if(accountDetail.Parent__c){
                    teamMemberList = createTeamMemberList(accountDetail.Id, teamMemberRole);
                    for (AccountTeamMember tm : teamMemberList){
                        teamMemberRole.add(tm.TeamMemberRole);
					}
                    if (!teamMemberRole.isEmpty() && teamMemberRole.size() != 4){
                        teamMemberList.addAll(createTeamMemberList(accountDetail.ParentId, teamMemberRole));
					}
                }
                else{
                    teamMemberList = createTeamMemberList(accountDetail.Id, teamMemberRole);
                    for (AccountTeamMember tm : teamMemberList){
                    	teamMemberRole.add(tm.TeamMemberRole);
					}
                    if (!teamMemberRole.isEmpty() && teamMemberRole.size() != 4){
                        for (AccountTeamMember tm : createTeamMemberList(accountDetail.ParentId, teamMemberRole)){
                            teamMemberRole.add(tm.TeamMemberRole);
                            teamMemberList.add(tm);
						}
                    }
                }
                
    // Create Account Team Member List
    public static List<AccountTeamMember> createTeamMemberList (Id accId, Set <String> teamMemberRole){
        Final List<String> roleList = Label.CCP_TeamMemberRole.split(',');
        return [SELECT User.Name, User.Email, user.Phone, TeamMemberRole, user.FullPhotoUrl, Account.Name, 
                FROM AccountTeamMember WHERE AccountId =: accId 
                AND TeamMemberRole NOT IN: (teamMemberRole) AND TeamMemberRole IN: (roleList)
                AND User.isActive = true];
    }
}

As you can see I have to use multiple iterations(For Loops) just to add Team Member Role to set of String, can you guys please suggest me some better way of doing this

Thanks
Best Answer chosen by Nirav_Shah
Abdul KhatriAbdul Khatri
This is the final version I can come that should fulfill your requirements. Please note that there is one issue with the end curly bracket. This atleast give you some idea of picking unique member of the role you need looping through accounts.
 
@RestResource(urlMapping='/getAccountTeamMember_api/*')
Global with sharing class CCP_RESTAccountTeamMemberController {
  //Wrapper to store the response
  global class TeamMember{
      Private List<rows> AccountTeamMember;
  }  
    
  global class rows {
    private string USER_NAME;
    private string EMAIL_ADDR;
    private string PHONE;
    private string ROLE;
    private string PROF_IMG_URL;
	private string ACC_NAME;
    private string UCM_ID;
  }
    
    /**
    * Send the response of Team Member Data
    * @return The updated REST response
    */
    @HttpGet
    global static List<TeamMember> getAccountTeamMember() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        
        string userName = req.params.get(CCP_GlobalConstant.user_name);
        
        try{
      		List<CCP_RESTAccountTeamMemberController.TeamMember> teamMembersDetail = new List<CCP_RESTAccountTeamMemberController.TeamMember>();
            CCP_RESTAccountTeamMemberController.TeamMember membersDetail = new CCP_RESTAccountTeamMemberController.TeamMember();
            List<Account> accountList = new List<Account>();


            if(String.isBlank(userName)) {
                res = CCP_RestResponseUtility.getRestResponse(res,417, system.label.CCP_Validation_for_Username);
                return;                
            }
            
            accountList = [select Id from Account where Id IN (SELECT AccountId FROM Users WHERE UserName = :userName) LIMIT 1];
            
            if(accountList.isEmpty()) {
                res = CCP_RestResponseUtility.getRestResponse(res,404, 'noUserFound');
                return;
            }

            List <AccountTeamMember> teamMemberList = new List <AccountTeamMember>();

               List<Id> idAccountList = getAccountHierarchyList(accountDetail.Id);

            Final List<String> roleList = Label.TeamMemberRole.split(',');

			Map<Id, List<AccountTeamMember>> MemberMap = getTeamMemberMap(idAccountList);

            for(Id id : idAccountList) {
                
                for (AccountTeamMember tm : MemberMap.get(id)) {
                    
                    if(teamMemberRole.contains(tm.TeamMemberRole)) continue;
                    
                    teamMemberRole.add(tm.TeamMemberRole);
                    teamMemberList.add(tm);
                    
                    if(teamMemberRole.size() == 4) break; 
                                    
                }
                
                if(teamMemberRole.size() == 4) break; 
            }

            List<CCP_RESTAccountTeamMemberController.rows> rows = new List<CCP_RESTAccountTeamMemberController.rows>();
            CCP_RESTAccountTeamMemberController.rows row;
            for (AccountTeamMember teamMember : teamMemberList)
            {
                row = new CCP_RESTAccountTeamMemberController.rows();
                row.USER_NAME = teamMember.User.Name;
                row.EMAIL_ADDR = teamMember.User.Email;
                row.PHONE = teamMember.user.Phone;
                row.ROLE = teamMember.TeamMemberRole;
                row.PROF_IMG_URL = teamMember.user.FullPhotoUrl;
                  row.ACC_NAME = teamMember.Account.Name;
                  rows.add(row);
            }
            membersDetail.AccountTeamMember = rows;
            teamMembersDetail.add(membersDetail);

            return teamMembersDetail;

        }catch (exception ex){
            return null;
        }
    }
    
        
    static List<Id> getAccountHierarchyList (Id accountId)
    {
        List<Id> idList = new List<Id>();
        Id idAccount = accountId;
        
        idList.add(idAccount);
        
        while(idAccount != null) {
            String parentAccount = null;
        
            for(Account account : [SELECT ParentId from Account where id = :idAccount]) {
                idList.add(account.ParentId);
            }
            idAccount = account.ParentId;
        }    
    }

    
    // Create Account Team Member List
    public static Map<Id, List<AccountTeamMember>> getTeamMemberMap (List<Id> idAcctList){
        
        Final List<String> roleList = Label.CCP_TeamMemberRole.split(',');
        Map<Id, List<AccountTeamMember>> returnMemberMap = new Map<Id, List<AccountTeamMember>>();
        
        for (AccountTeamMember member : [SELECT User.Name, User.Email, user.Phone, TeamMemberRole, user.FullPhotoUrl, Account.Name, 
                                                    FROM AccountTeamMember 
                                                    WHERE AccountId =: idAcctList 
                                                        AND TeamMemberRole IN: (roleList)
                                                        AND User.isActive = true])
        {
                                                            
            if(returnMemberMap.contains(member.AccountId)) {
                
                List<AccountTeamMember> tempMemberList = returnMemberMap.get(member.AccountId);
                tempMemberList.add(member);
                returnMemberMap.put(member.AccountId, tempMemberList);
            }else{
                returnMemberMap.put(member.AccountId, new List<AccountTeamMember>{ member });
            }
                                             
        }
        
        return returnMemberMap;
    
    } 
}

 

All Answers

Abdul KhatriAbdul Khatri
Can you please share the complete code. Your code is quite confusing with too many if's and for's. Then you just else where much of the code is similar in all the if's so what's the point of having the first 2 ifs

Where is the accoutnDtail is coming from?
Is this code calling form trigger?

What exactly is getting set in the teamMemberRole which are passing to createTeamMemberList as it is just getting intialized?

Please tell what excatly you are trying to do here.
Nirav_ShahNirav_Shah
Hi Abdul,
Below is the complete code...
 
@RestResource(urlMapping='/getAccountTeamMember_api/*')
Global with sharing class CCP_RESTAccountTeamMemberController {
  //Wrapper to store the response
  global class TeamMember{
    Private List<rows> AccountTeamMember;
    }  
  global class rows {
    private string USER_NAME;
    private string EMAIL_ADDR;
    private string PHONE;
    private string ROLE;
    private string PROF_IMG_URL;
        private string ACC_NAME;
        private string UCM_ID;
  }
    
    /**
    * Send the response of Team Member Data
    * @return The updated REST response
    */
    @HttpGet
    global static List<TeamMember> getAccountTeamMember() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        
        string userName = req.params.get(CCP_GlobalConstant.user_name);
        
        try{
      List<CCP_RESTAccountTeamMemberController.TeamMember> teamMembersDetail = new List<CCP_RESTAccountTeamMemberController.TeamMember>();
            CCP_RESTAccountTeamMemberController.TeamMember membersDetail = new CCP_RESTAccountTeamMemberController.TeamMember();
            List<Account> accountList = new List<Account>();
            
            if(String.isNotBlank(userName)){
                List<User> userList = [Select Id, Contact.AccountId from User where userName = :userName limit 1];
                if(!userList.isEmpty()){
                    accountList = [select Id ,Name,Ultimate_Parent__c,Parent__c, parentId ,Parent.ParentId 
                                   from Account where Id =: userList[0].Contact.AccountId LIMIT 1];
                }
                else{
                    res = CCP_RestResponseUtility.getRestResponse(res,404, 'noUserFound');
                  return null;
                }
            }
            else{
                res = CCP_RestResponseUtility.getRestResponse(res,417, system.label.CCP_Validation_for_Username);
                return null;
            }
            
            List <AccountTeamMember> teamMemberList = new List <AccountTeamMember>();
            
            if(!accountList.isEmpty()){
                Account accountDetail = accountList[0];
                Set <String> teamMemberRole = new Set <String>();
                
                if(accountDetail.Ultimate_Parent__c && accountDetail.Parent__c){
                    teamMemberList = createTeamMemberList(accountDetail.Id, teamMemberRole);
                }
                else if(accountDetail.Parent__c){
                    teamMemberList = createTeamMemberList(accountDetail.Id, teamMemberRole);
                    for (AccountTeamMember tm : teamMemberList){
                        teamMemberRole.add(tm.TeamMemberRole);
          }
                    if (!teamMemberRole.isEmpty() && teamMemberRole.size() != 4){
                        teamMemberList.addAll(createTeamMemberList(accountDetail.ParentId, teamMemberRole));
          }
                }
                else{
                    teamMemberList = createTeamMemberList(accountDetail.Id, teamMemberRole);
                    for (AccountTeamMember tm : teamMemberList){
                      teamMemberRole.add(tm.TeamMemberRole);
          }
                    if (!teamMemberRole.isEmpty() && teamMemberRole.size() != 4){
                        for (AccountTeamMember tm : createTeamMemberList(accountDetail.ParentId, teamMemberRole)){
                            teamMemberRole.add(tm.TeamMemberRole);
                            teamMemberList.add(tm);
            }
                    }
                    if (!teamMemberRole.isEmpty() && teamMemberRole.size() != 4){
                        teamMemberList.addAll(createTeamMemberList(accountDetail.Parent.ParentId, teamMemberRole));
                    }
                }
                
                List<CCP_RESTAccountTeamMemberController.rows> rows = new List<CCP_RESTAccountTeamMemberController.rows>();
                CCP_RESTAccountTeamMemberController.rows row;
                for (AccountTeamMember teamMember : teamMemberList){
                    row = new CCP_RESTAccountTeamMemberController.rows();
                    row.USER_NAME = teamMember.User.Name;
                    row.EMAIL_ADDR = teamMember.User.Email;
                    row.PHONE = teamMember.user.Phone;
                    row.ROLE = teamMember.TeamMemberRole;
                    row.PROF_IMG_URL = teamMember.user.FullPhotoUrl;
          row.ACC_NAME = teamMember.Account.Name;
          rows.add(row);
                }
                membersDetail.AccountTeamMember = rows;
                teamMembersDetail.add(membersDetail);
            }
            return teamMembersDetail;
        }catch (exception ex){
            return null;
        }
    }
    // Create Account Team Member List
    public static List<AccountTeamMember> createTeamMemberList (Id accId, Set <String> teamMemberRole){
        Final List<String> roleList = Label.TeamMemberRole.split(',');
        return [SELECT User.Name, User.Email, user.Phone, TeamMemberRole, user.FullPhotoUrl, Account.Name, 
                FROM AccountTeamMember WHERE AccountId =: accId 
                AND TeamMemberRole NOT IN: (teamMemberRole) AND TeamMemberRole IN: (roleList)
                AND User.isActive = true];
    }
}

The requirement is I need to expose the team members details to external team and for that I have created this rest resource. We have three level hierarchy in Account, Ultimate Parent Account, Parent Account and Child Account. Each Account has team members associated with it and I wanted to expose some specific team member.
 

I have three levels of Account Hierarchy and all three account has Team Member associated with it. I want to fetch the user/team member with role AE, AM and SE Account
Account : A1 -> Ultimate Parent Account,
Team Member : T1 Role : AE, T2 Role : AM, T3 Role : SE
A2 -> Parent Account
Team Member : T1 Role : AE, T2 Role :AM
A3 -> Child Account
Team Member : T1 Role : AE
(A3 -> A2 -> A1) A3 is Child, A2 is Parent of A3 and A1 is Parent of A2
Now let's consider following scenario, let's say Account -> Child Account If the Child Account has the above roles, then bring up all individuals associated with these 3 roles. If the CA does not have individuals associated with any of these roles, go to its Parent Account and get the names of the individuals for that role. If the PA also does not have individuals associated with that missing role, go to its Ultimate Parent Account for the name against the role.

Thanks

Abdul KhatriAbdul Khatri
Hi Nirav

Sorry it took little time. This is how I think will work in your case. I have tried but I hope it work, at least it will give some design thought

One thing you are checking Team Role with 4 and with 3.

I added the following new Method. It will in first place get you based on which account you are on. It will work upwards.
static List<Id> getAccountHierarchyList (Id accountId)
{
	List<Id> idList = new List<Id>();
    Id idAccount = accountId;
    
    while(idAccount != null) {
        String parentAccount = null;
    
        for(Account account : [SELECT ParentId from Account where id = :idAccount]) {
            idList.add(account.ParentId);
        }
        idAccount = account.ParentId;
    }    
}

Now I change your entire code 
if(accountDetail.Ultimate_Parent__c && accountDetail.Parent__c){
                    teamMemberList = createTeamMemberList(accountDetail.Id, teamMemberRole);
                }
                else if(accountDetail.Parent__c){
                    teamMemberList = createTeamMemberList(accountDetail.Id, teamMemberRole);
                    for (AccountTeamMember tm : teamMemberList){
                        teamMemberRole.add(tm.TeamMemberRole);
          }
                    if (!teamMemberRole.isEmpty() && teamMemberRole.size() != 4){
                        teamMemberList.addAll(createTeamMemberList(accountDetail.ParentId, teamMemberRole));
          }
                }
                else{
                    teamMemberList = createTeamMemberList(accountDetail.Id, teamMemberRole);
                    for (AccountTeamMember tm : teamMemberList){
                      teamMemberRole.add(tm.TeamMemberRole);
          }
                    if (!teamMemberRole.isEmpty() && teamMemberRole.size() != 4){
                        for (AccountTeamMember tm : createTeamMemberList(accountDetail.ParentId, teamMemberRole)){
                            teamMemberRole.add(tm.TeamMemberRole);
                            teamMemberList.add(tm);
            }
                    }
                    if (!teamMemberRole.isEmpty() && teamMemberRole.size() != 4){
                        teamMemberList.addAll(createTeamMemberList(accountDetail.Parent.ParentId, teamMemberRole));
                    }
                }
To this
                List<Id> idAccountList = getAccountHierarchyList(accountDetail.Id);
                for(Id id : idAccountList) {
					List <AccountTeamMember> tempTeamMemberList = createTeamMemberList (id, teamMemberRole);

                    for (AccountTeamMember tm : tempTeamMemberList){
                        teamMemberRole.add(tm.TeamMemberRole);
          			}

                    teamMemberList.addAll(tempTeamMemberList);

                    if (!teamMemberRole.isEmpty() && teamMemberRole.size() == 4) break;
                }

Let me know your thought.

Nirav_ShahNirav_Shah
Hey Abdul,

Thanks for your response.

My concern here is, there are multiple for loops in which SOQL query are being executed which can be resulted in Governer Limit.
I have talked with my lead and what he suggested is to take all the TeamMembers in one Map (Including Ultimate Parent Account, Parent Account, and Child Account) and then iterate on that map to get a result. But I don't have much idea on how to achieve that. If you can please give your inputs for the same.

Thanks
 
Abdul KhatriAbdul Khatri
Since your webservice is limiting to only single account and it's child,  with the changes provided I won't be really concern on that Governor Limit. But there is always room for improvement and I reduces the code futher below with making it little cleaner.

I think your requirement is to stop looking at the parent if you find your desire roles on the child.
 
            if(String.isBlank(userName)) {
                res = CCP_RestResponseUtility.getRestResponse(res,417, system.label.CCP_Validation_for_Username);
                return;                
            }
            
            accountList = [select Id from Account where Id IN (SELECT AccountId FROM Users WHERE UserName = :userName) LIMIT 1];
            
            if(accountList.isEmpty()) {
                res = CCP_RestResponseUtility.getRestResponse(res,404, 'noUserFound');
                return;
            }

            List <AccountTeamMember> teamMemberList = new List <AccountTeamMember>();

               List<Id> idAccountList = getAccountHierarchyList(accountDetail.Id);

            Final List<String> roleList = Label.TeamMemberRole.split(',');

            List<AccountTeamMember> teamMembersList = [SELECT User.Name, User.Email, user.Phone, TeamMemberRole, user.FullPhotoUrl, Account.Name, 
                                                        FROM AccountTeamMember 
                                                           WHERE AccountId = :idAccountList 
                                                                AND TeamMemberRole IN: (roleList)
                                                                AND User.isActive = true];

            if(teamMembersList == null) return;

               for(AccountTeamMember tm : teamMembersList) {
                teamMemberRole.add(tm.TeamMemberRole)                
                   teamMemberList.add(tm);                
            }

               List<CCP_RESTAccountTeamMemberController.rows> rows = new List<CCP_RESTAccountTeamMemberController.rows>();
               CCP_RESTAccountTeamMemberController.rows row;
               for (AccountTeamMember teamMember : teamMemberList)
            {
                row = new CCP_RESTAccountTeamMemberController.rows();
                row.USER_NAME = teamMember.User.Name;
                row.EMAIL_ADDR = teamMember.User.Email;
                row.PHONE = teamMember.user.Phone;
                row.ROLE = teamMember.TeamMemberRole;
                row.PROF_IMG_URL = teamMember.user.FullPhotoUrl;
                  row.ACC_NAME = teamMember.Account.Name;
                  rows.add(row);
            }
            membersDetail.AccountTeamMember = rows;
            teamMembersDetail.add(membersDetail);

            return teamMembersDetail;
Nirav_ShahNirav_Shah
Hi Abdul,

My requirement is to find the desired role from all the level.
For E.g.

I have three levels of Account Hierarchy and all three account has Team Member associated with it. I want to fetch the team member with role AE, AM and SE

Account : A1 -> Parent Account,
Team Member : T1 Role : AE, T2 Role : AM, T3 Role : SE
A2 -> Sub Parent Account
Team Member : T4 Role : AE, T5 Role :AM
A3 -> Child Account
Team Member : T6 Role : AE
(A3 -> A2 -> A1) A3 is Child, A2 is Parent of A3 and A1 is Parent of A2

Now let's consider following scenario, let's say

Account -> Child Account
  • If the Child Account has the above roles, then bring up all individuals associated with these 3 roles. If the CA does not have individuals associated with any of these roles, go to its Sub Parent Account and get the names of the individuals for that missing role. If the SPA also does not have individuals associated with that missing role, go to its Parent Account for the name against the role.
  • it should give result as T6 Role AE, T5 Role AM, and T3 Role SE
Account -> Sub Parent Account
  • If the Sub Parent Account has the above roles, then bring up all individuals associated with these 3 roles. If the SPA does not have individuals associated with any of these roles, go to its Sub Parent Account and get the names of the individuals for that missing role.
  • it should give result as T4 Role AE, T5 Role AM, and T3 Role SE
Account -> Parent Account
  • If the Parent Account has the above roles, then bring up all individuals associated with these 3 roles. If the PA does not have individuals associated with any of these roles, leave it blank.
  • it should give result as T1 Role AE, T2 Role AM, and T3 Role SE
Abdul KhatriAbdul Khatri
This is the final version I can come that should fulfill your requirements. Please note that there is one issue with the end curly bracket. This atleast give you some idea of picking unique member of the role you need looping through accounts.
 
@RestResource(urlMapping='/getAccountTeamMember_api/*')
Global with sharing class CCP_RESTAccountTeamMemberController {
  //Wrapper to store the response
  global class TeamMember{
      Private List<rows> AccountTeamMember;
  }  
    
  global class rows {
    private string USER_NAME;
    private string EMAIL_ADDR;
    private string PHONE;
    private string ROLE;
    private string PROF_IMG_URL;
	private string ACC_NAME;
    private string UCM_ID;
  }
    
    /**
    * Send the response of Team Member Data
    * @return The updated REST response
    */
    @HttpGet
    global static List<TeamMember> getAccountTeamMember() {
        RestRequest req = RestContext.request;
        RestResponse res = RestContext.response;
        
        string userName = req.params.get(CCP_GlobalConstant.user_name);
        
        try{
      		List<CCP_RESTAccountTeamMemberController.TeamMember> teamMembersDetail = new List<CCP_RESTAccountTeamMemberController.TeamMember>();
            CCP_RESTAccountTeamMemberController.TeamMember membersDetail = new CCP_RESTAccountTeamMemberController.TeamMember();
            List<Account> accountList = new List<Account>();


            if(String.isBlank(userName)) {
                res = CCP_RestResponseUtility.getRestResponse(res,417, system.label.CCP_Validation_for_Username);
                return;                
            }
            
            accountList = [select Id from Account where Id IN (SELECT AccountId FROM Users WHERE UserName = :userName) LIMIT 1];
            
            if(accountList.isEmpty()) {
                res = CCP_RestResponseUtility.getRestResponse(res,404, 'noUserFound');
                return;
            }

            List <AccountTeamMember> teamMemberList = new List <AccountTeamMember>();

               List<Id> idAccountList = getAccountHierarchyList(accountDetail.Id);

            Final List<String> roleList = Label.TeamMemberRole.split(',');

			Map<Id, List<AccountTeamMember>> MemberMap = getTeamMemberMap(idAccountList);

            for(Id id : idAccountList) {
                
                for (AccountTeamMember tm : MemberMap.get(id)) {
                    
                    if(teamMemberRole.contains(tm.TeamMemberRole)) continue;
                    
                    teamMemberRole.add(tm.TeamMemberRole);
                    teamMemberList.add(tm);
                    
                    if(teamMemberRole.size() == 4) break; 
                                    
                }
                
                if(teamMemberRole.size() == 4) break; 
            }

            List<CCP_RESTAccountTeamMemberController.rows> rows = new List<CCP_RESTAccountTeamMemberController.rows>();
            CCP_RESTAccountTeamMemberController.rows row;
            for (AccountTeamMember teamMember : teamMemberList)
            {
                row = new CCP_RESTAccountTeamMemberController.rows();
                row.USER_NAME = teamMember.User.Name;
                row.EMAIL_ADDR = teamMember.User.Email;
                row.PHONE = teamMember.user.Phone;
                row.ROLE = teamMember.TeamMemberRole;
                row.PROF_IMG_URL = teamMember.user.FullPhotoUrl;
                  row.ACC_NAME = teamMember.Account.Name;
                  rows.add(row);
            }
            membersDetail.AccountTeamMember = rows;
            teamMembersDetail.add(membersDetail);

            return teamMembersDetail;

        }catch (exception ex){
            return null;
        }
    }
    
        
    static List<Id> getAccountHierarchyList (Id accountId)
    {
        List<Id> idList = new List<Id>();
        Id idAccount = accountId;
        
        idList.add(idAccount);
        
        while(idAccount != null) {
            String parentAccount = null;
        
            for(Account account : [SELECT ParentId from Account where id = :idAccount]) {
                idList.add(account.ParentId);
            }
            idAccount = account.ParentId;
        }    
    }

    
    // Create Account Team Member List
    public static Map<Id, List<AccountTeamMember>> getTeamMemberMap (List<Id> idAcctList){
        
        Final List<String> roleList = Label.CCP_TeamMemberRole.split(',');
        Map<Id, List<AccountTeamMember>> returnMemberMap = new Map<Id, List<AccountTeamMember>>();
        
        for (AccountTeamMember member : [SELECT User.Name, User.Email, user.Phone, TeamMemberRole, user.FullPhotoUrl, Account.Name, 
                                                    FROM AccountTeamMember 
                                                    WHERE AccountId =: idAcctList 
                                                        AND TeamMemberRole IN: (roleList)
                                                        AND User.isActive = true])
        {
                                                            
            if(returnMemberMap.contains(member.AccountId)) {
                
                List<AccountTeamMember> tempMemberList = returnMemberMap.get(member.AccountId);
                tempMemberList.add(member);
                returnMemberMap.put(member.AccountId, tempMemberList);
            }else{
                returnMemberMap.put(member.AccountId, new List<AccountTeamMember>{ member });
            }
                                             
        }
        
        return returnMemberMap;
    
    } 
}

 
This was selected as the best answer
Nirav_ShahNirav_Shah
Thanks, Abdul,
This works after some Modification