0

How can I create a variable for the aggregateresult addedserviceresults that would show in a Visualforce table as a column % for each Franchise_Added_Service__r.Group__c out of the value from a second aggregateresult inspections.['c']? So for example, if the group is Radon and there are 7 records, and the count of records in the inspections aggregate result is 10, I would want to show 70% for this row in the table, but if the service is WDI (2 records), I would want to show 20%, and so on...

public AggregateResult[] inspections {
        get {
            return [SELECT Count(id) c,Sum(Number_of_Home_Inspections__c) HomeInspections, Sum(Home_Inspection_dollars__c) Dollars, Franchise__c,
                    Sum(Plus_Sales__c) plus_dollars, Sum(Premium_Sales__c) premium_dollars, Sum(Prestige_Sales__c) prestige_dollars,
                    Sum(of_Plus_Sales__c) plus_count, Sum(of_Premium_Sales__c) premium_count, Sum(of_Prestige_Sales__c) prestige_count,
                    Sum(Added_Services__c) addedservices, Sum(Standalones__c) standalones
                    FROM Inspection__c 
                    WHERE Test_Inspection__c = false AND Status__c = 'Completed' AND Franchise__c =: accountid
                    GROUP BY Franchise__c];
        }        
    }

    public AggregateResult[] AddedServiceResults {
        get {
            return [SELECT Count(Id) Total, Sum(Price_Sold__c) Dollars,Franchise_Added_Service__r.Group__c a
                    FROM Inspection_Service__c 
                    WHERE Inspection__r.Status__c = 'Completed' AND Inspection__r.Test_Inspection__c = false AND Inspection_Franchise__c =: accountid
                    GROUP BY Franchise_Added_Service__r.Group__c];
        }        
    }
1
  • What you have tried at visualforce code Commented Sep 19, 2018 at 4:07

1 Answer 1

0

I got it to work with this:

public AggregateResult[] inspections {
        get {
            return [SELECT Count(id) c,Sum(Number_of_Home_Inspections__c) HomeInspections, Sum(Home_Inspection_dollars__c) Dollars, Franchise__c,
                    Sum(Plus_Sales__c) plus_dollars, Sum(Premium_Sales__c) premium_dollars, Sum(Prestige_Sales__c) prestige_dollars,
                    Sum(of_Plus_Sales__c) plus_count, Sum(of_Premium_Sales__c) premium_count, Sum(of_Prestige_Sales__c) prestige_count,
                    Sum(Added_Services__c) addedservices, Sum(Standalones__c) standalones
                    FROM Inspection__c 
                    WHERE Test_Inspection__c = false AND Status__c = 'Completed' AND Franchise__c =: accountid
                    GROUP BY Franchise__c];
        }        
    }

    public AggregateResult[] AddedServiceResults {
        get {
            return [SELECT Count(Id) Total, Sum(Price_Sold__c) Dollars,Franchise_Added_Service__r.Group__c a
                    FROM Inspection_Service__c 
                    WHERE Inspection__r.Status__c = 'Completed' AND Inspection__r.Test_Inspection__c = false AND Inspection_Franchise__c =: accountid
                    GROUP BY Franchise_Added_Service__r.Group__c];
        }        
    }

    public FranchiseMetrics(){
        AddedServices = new List<AddedService>();
        for(AggregateResult a : AddedServiceResults) {
            for(AggregateResult b : inspections){
                String AddedServiceGroup = (String)a.get('a');    
                AddedServices.add(new AddedService(AddedServiceGroup, a, b)); 
            }
        }
    }

    public class AddedService {
        public String AddedServiceGroup {get;set;}
        public Decimal TotalInspections {get;set;}
        public Decimal TotalServices {get;set;}
        public Decimal Dollars {get;set;}
        public Decimal InspectionServiceUpgradeRate{get{ return (TotalServices/TotalInspections).setScale(3) * 100;}} 

        public AddedService(String Name, AggregateResult a, AggregateResult b) {
            AddedServiceGroup = Name;
            TotalServices =  (Decimal)a.get('Total');
            TotalInspections = (Decimal)b.get('c');
            Dollars = (Decimal)a.get('Dollars');
        }
    }

Then in Visualforce I displayed the values of AddedServices in a pageblocktable

You must log in to answer this question.

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