3

I have two questions, see code below:

  1. Why I can't add Owner.SmallPhotoUrl into the Aggregate Result, and how else can I get the show the SmallPhotoUrl in my Visualforcepage for the specific Sales Reps.
  2. How can I test the wrapper class in the testmethod: SalesRepLeaderboard.SalesRepTotalWrappers[] wraps = new SalesRepLeaderboard.SalesRepTotalWrappers();

    public with sharing class SalesRepLeaderboard {
    
    public SalesRepTotalWrapper[] SalesRepTotalWrappers { get; set; }
    
    public SalesRepLeaderboard() {
        AggregateResult[] oppaggregate = [SELECT sum(Amount) SalesRepTotal, Owner.Name 
            FROM Opportunity 
            WHERE StageName='Closed Won' AND LastModifiedDate=Today AND Owner.Profile.Name='Rocket Lawyer Sales' 
            GROUP BY Owner.Name 
            ORDER BY sum(Amount) DESC NULLS LAST];
        SalesRepTotalWrappers = new List<SalesRepTotalWrapper>();
        for (AggregateResult ar : oppaggregate)  
        {
            SalesRepTotalWrappers.add(new SalesRepTotalWrapper(ar));
        }
    }
    
    public class SalesRepTotalWrapper {
        public Decimal TotalSales { get; private set; }
        public String Name { get; private set; }
    
        public SalesRepTotalWrapper(AggregateResult ar) {
            TotalSales = (Decimal) ar.get('SalesRepTotal');
            Name = (String) ar.get('Name');
        }
    }
    
    }
    

1 Answer 1

4

(1) You can't access Owner.SmallPhotoUrl in the query, because you're not aggregating on it. What you need to do is get the OwnerId's from the opportunities, and then pull the names and small photo URL's. Like this:

public with sharing class SalesRepLeaderboard {
    public SalesRepTotalWrapper[] SalesRepTotalWrappers { get; set; }

    public SalesRepLeaderboard() {
        // Group on OwnerId - more reliable than Owner.Name in any case - 
        // what if you had two reps with the same name?
        AggregateResult[] oppaggregate = [SELECT sum(Amount) SalesRepTotal, OwnerId
            FROM Opportunity 
            WHERE StageName='Closed Won' AND LastModifiedDate=Today AND Owner.Profile.Name='Rocket Lawyer Sales' 
            GROUP BY OwnerId
            ORDER BY sum(Amount) DESC NULLS LAST];

        // Make a list of the sales rep IDs
        List<ID> salesRepIds = new List<ID>();
        for (AggregateResult ar : oppaggregate) {
            salesRepIds.add((ID)ar.get('OwnerId'));
        }

        // Get the names and photo URL's for those reps
        Map<Id, User> salesRepsMap = new Map<ID, User>([SELECT Name, SmallPhotoUrl 
            FROM User 
            WHERE Id IN :salesRepIds]);

        // Make our list of wrappers
        SalesRepTotalWrappers = new List<SalesRepTotalWrapper>();
        for (AggregateResult ar : oppaggregate) {
            User salesRep = salesRepsMap.get((ID)ar.get('OwnerId'));
            SalesRepTotalWrappers.add(new SalesRepTotalWrapper((Decimal) ar.get('SalesRepTotal'), 
                salesRep.Name, 
                salesRep.SmallPhotoUrl));
        }
    }

    public class SalesRepTotalWrapper {
        public Decimal TotalSales { get; private set; }
        public String Name { get; private set; }
        public String SmallPhotoUrl { get; private set; }

        public SalesRepTotalWrapper(Decimal TotalSales, String Name, String SmallPhotoUrl) {
            this.TotalSales = TotalSales;
            this.Name = Name;
            this.SmallPhotoUrl = SmallPhotoUrl;
        }
    }
}

(2) In your test method, you will need to create some users and associated opportunities with a known total value per user, create your leaderboard, then verify (using System.assert()) that leaderboard.SalesRepTotalWrappers contains the correct rep names, URLs and totals.

4
  • Pat again thanks for your quick response, it works perfectly!I always try to do as much in one SOQL query but sometimes it is just not possible. Commented Oct 15, 2012 at 16:44
  • Thanks for the comment and the 'accept'. An upvote also would be traditional ;-) Cheers!
    – metadaddy
    Commented Oct 15, 2012 at 17:23
  • I can give you an upvote as soon as I have reputation > 15 points. Commented Oct 15, 2012 at 21:30
  • No worries - I saw you were a new user, so I thought I'd explain the process :-)
    – metadaddy
    Commented Oct 16, 2012 at 0:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .