function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Yogesh BiyaniYogesh Biyani 

Remove AccountTeams from Accounts

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'));
}



 
MagulanDuraipandianMagulanDuraipandian
Extract using data loader for backup. Use data loader to delete it.
--
Magulan Duraipandian
www.infallibletechie.com
Yogesh BiyaniYogesh Biyani
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
MagulanDuraipandianMagulanDuraipandian
Yogesh,
It will be hard to do it in the file.
If you have access to SQL tables in your company's data warehouse, create a temp table and analyze it by running SQL queries.
--
Magulan Duraipandian
www.infallibletechie.com