ApexSensei

Drills SOQL queries

Summarize Opportunity amounts

COUNT() counts rows and must sit alone in its SELECT.

Your task

Summarize three tagged Opportunities, including one with a null Amount.

String tag = 'AS-Q310-' + UserInfo.getUserId() + '-' +
    String.valueOf(Datetime.now().getTime());
String stageName = Opportunity.StageName.getDescribe().getPicklistValues()[0].getValue();
Account parent = new Account(Name = tag);
insert parent;
List<Opportunity> seeded = new List<Opportunity>{
    new Opportunity(Name = tag + '-100', AccountId = parent.Id, StageName = stageName,
        CloseDate = Date.today(), Amount = 100),
    new Opportunity(Name = tag + '-300', AccountId = parent.Id, StageName = stageName,
        CloseDate = Date.today(), Amount = 300),
    new Opportunity(Name = tag + '-Null', AccountId = parent.Id, StageName = stageName,
        CloseDate = Date.today(), Amount = null)
};
insert seeded;

Requirements

  1. SELECT COUNT() FROM Opportunity should be present
  2. Row count should be computed from the query
  3. COUNT(Amount), SUM(Amount) and AVG(Amount) should be present
  4. Aggregate rows: should be 3
  5. Aggregate nonnull: should be 2
  6. Aggregate sum: should be true
  7. Aggregate average: should be true

Sign in to open this drill