function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
arandallarandall 

Displaying Aggregate Data in a VF Email Template

Hello,

 

I've got data that I'd like to aggregate and display in a VF email that I'm sending out.  I've been able to display unaggregated data in the email, but when I try to aggregate, I receive errors.

 

From looking at previous posts, I gather that I can't display the data directly from the AggregateResult list that my query generates.  I've attempted to use a wrapper class to remedy this, but when I test my VF email template, I receive the following message: "Error occurred trying to load the template for preview: Attempt to de-reference a null object."

 

Here's my class (I've excluded some code - please let me know if it would be helpful to include it)

public class AlexsClass{


    public Id thisContactId {get;set;}
    public List<Class_Implementation__c> Classlist;
    public List<AggregateResult> Metriclist;
    public List<Summary> Summaries;
    
/*...collecting data that allows me to perform my SOQL query...*/
    
    //A wrapper class that is supposed to capture data from the Aggregate Results list
    public class Summary{
        public String Percent_A {get; set;}
        public String Grade {get; set;}
    
       public Summary(string a, string g){
            this.Percent_A=a;
            this.Grade=g;
       }
    }
    
    //Aggregating the class metrics
    public List<AggregateResult> getMetriclist() {
        if (Metriclist == null) {
            List<AggregateResult> Metriclist =
            [SELECT AVG(A__c) A__c, Start_Date__c,
            COUNT(Class_Implementation__r.Id) Id, COUNT(class_grade__c) Class_Grade
            FROM Class_Metric__c
            WHERE Class_Implementation__r.Id IN :getClasslist()
            GROUP BY Start_Date__c];
        }
        return Metriclist;
    }
    
    //Attempting to wrap the aggregates in the new class
    public List<Summary> summaries = new List<Summary>();

    public List<Summary> getSummaries(){
        for (AggregateResult metrics : Metriclist) { 
            summaries.add(new Summary((String.valueOf(metrics.get('A__c'))), String.valueOf(metrics.get('class_grade'))));
            }
        return summaries;
    }
}

 

Here's my VF component:

<apex:component controller="AlexsClass" access="global">
<apex:attribute name="ContactID" description="This is the contact Id." type="Id" assignTo="{!thisContactId}"/>


[ %A ] - [ % of scheduled time online ] - [ Hours Online ]

<apex:repeat var="sum" value="{!Summaries}">

[ {!sum.Grade} ] [ {!sum.Percent_A} ]

</apex:repeat>

</apex:component>

 

And here's my VF Email template:

<messaging:emailTemplate subject="Here are your metrics!" recipientType="Contact" relatedToType="School_Implementation__c">
<messaging:PlainTextEmailBody >
Dear {!recipient.salutation} {!recipient.lastname},

<c:Alex_s_Controller ContactID="{!recipient.Id}"/>

</messaging:PlainTextEmailBody>   
</messaging:emailTemplate>

 

Any help you can provide would be greatly appreciated!

Cloud Advisory GroupCloud Advisory Group
Dear Arandall where you able to find a solution for this? I am having the similar problems and was hoping you may have solved it! You can see my post here http://boards.developerforce.com/t5/Visualforce-Development/VF-Email-Wrapper-Class-Woes/m-p/608893/highlight/true#M63266