Skip to main content Join the Agentforce Virtual Hackathon to build innovative solutions and compete for a $50k Grand Prize. Sign up now. Terms apply.

We have some accounts with incorrect account team and the following query identifies all such accounts. Basically, accounts with two or more account team members with the Distributor role has incorrect teams.

Select AccountId, Account.Name, count(id) from AccountTeamMember WHERE TeamMemberRole ='Distributor License Share' AND UserId!='0051W000004SVNhQAO' Group BY AccountId, Account.Name HAVING count(id) >=2 LIMIT 2000

What is the easiest way to delete all such account teams? 

I am still learning Apex/SOQL; how do I collect the AccountIds from the above aggregate result and pass it to get all the AccountTeamMember IDs to delete? The following code in developer console fails with the following error

Invalid identifier ' '. Apex identifiers must start with an ASCII letter (a-z or A-Z) followed by any number of ASCII letters (a-z or A-Z), digits (0 - 9), '$', '_'.​​​​​​​

List<AggregateResult> results = [Select AccountId, Account.Name, count(id) from AccountTeamMember

WHERE TeamMemberRole ='Distributor License Share' AND UserId!='0051W000004SVNhQAO'

Group BY AccountId, Account.Name HAVING count(id) >=2 LIMIT 2000 ];

System.debug(results.size());

System.debug(results[0]);

Set<id > AccountIds = new Set<id>();

      

for(AggregateResult exp: results){

    AccountIds.add((Id)exp.get('AccountId'));

}

 
3 answers
  1. May 21, 2019, 3:39 AM
    Hello Magulan, 

    Thanks for the suggestion. However, I am trying to get to a subset of records which caused the issue so that we can fix the root cause of the problem.  We have a custom object associated with a contact and a trigger on this custom object adds the team member to the contact's account. Some of the contacts are associated with dummy accounts and the trigger ends up assigning multiple team members to the same account. So we need to find such contacts and add them to unique accounts. 

    Is there a way to save a list of records to a file and then use this list in a query to find related records? 

    Yogesh
0/9000