
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 errorInvalid 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'));
}
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 Duraipandianwww.infallibletechie.com