Find your content:

Search form

You are here

Aggregate Owner.SmallPhotoUrl and Calling Wrapper Test Method

 
Share

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

Attribution to: Thys Michels

Possible Suggestion/Solution #1

(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.


Attribution to: metadaddy
This content is remixed from stackoverflow or stackexchange. Please visit https://salesforce.stackexchange.com/questions/2284

My Block Status

My Block Content