Keep busy Account groups
GROUP BY returns one row per value.
Your task
Group this run's Opportunities by Account and keep only Accounts with two or more.
String tag = 'AS-Q311-' + UserInfo.getUserId() + '-' +
String.valueOf(Datetime.now().getTime());
String stageName = Opportunity.StageName.getDescribe().getPicklistValues()[0].getValue();
List<Account> parents = new List<Account>{
new Account(Name = tag + '-Three'),
new Account(Name = tag + '-Two'),
new Account(Name = tag + '-One')
};
insert parents;
insert new List<Opportunity>{
new Opportunity(Name = tag + '-A1', AccountId = parents[0].Id,
StageName = stageName, CloseDate = Date.today()),
new Opportunity(Name = tag + '-A2', AccountId = parents[0].Id,
StageName = stageName, CloseDate = Date.today()),
new Opportunity(Name = tag + '-A3', AccountId = parents[0].Id,
StageName = stageName, CloseDate = Date.today()),
new Opportunity(Name = tag + '-B1', AccountId = parents[1].Id,
StageName = stageName, CloseDate = Date.today()),
new Opportunity(Name = tag + '-B2', AccountId = parents[1].Id,
StageName = stageName, CloseDate = Date.today()),
new Opportunity(Name = tag + '-C1', AccountId = parents[2].Id,
StageName = stageName, CloseDate = Date.today())
};Requirements
- Group count should be computed from the query
GROUP BY AccountIdshould be presentHAVING COUNT(Id) >= 2should be presentGroups kept:should be2Groups high:should betrueGroups exactly two:should betrueGroups below excluded:should betrue