• Cloud Advisory Group
  • NEWBIE
  • 0 Points
  • Member since 2013

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies

I am attempting to build a visualforce email template, controller, and component to basically act a a catch all of object records so that we can build any type of email relating to a transaction.

 

We are working with 7 different objects, and we would like to be able to show any records from a sale instance. The way we have accomplished this on other visualforce pages is through wrapper classes, unfortunately the same slighlty modified code does not work for a visualforce email component and controller. The workflow works and sends an email but the subject is gone and there is no body. Below is my code, I have found that if I comment out in the controller everything after line 48 it sends some test variables fine, but as soon as the wrapper classes are actually interacted with the whole thing breaks. There is also apparently no way to debug controllers used in an email's component?

 

Any help you can offer is greatly appreciated.

 

Thank you.

 

EMAIL TEMPLATE

<messaging:emailTemplate subject="Service Receipt" recipientType="Contact" relatedToType="CAG_Transaction__c">
    
    <messaging:htmlEmailBody >
    <c:cagSalesReceipt tId="{!relatedto.id}"/>

    </messaging:htmlEmailBody >
    
</messaging:emailTemplate>

 COMPONENT

<apex:component controller="cag_email_transaction_controller" access="global">
    <apex:attribute name="tid" type="Id" description="Transaction Id" assignTo="{!transid}"/>

  		<apex:repeat value="{!tlist}" var="tt">
  		your transaction id:{!tt.id}
  		</apex:repeat>
  		Your Sale id{!s.id}

</apex:component>

 CONTROLLER

public with sharing class cag_email_transaction_controller {    
  public cag_sale__c s {get;set;}
  public CAG_Transaction__c t {get;set;}
  public string saleid {get;set;}
  public id transid {get;set;}

  public cag_email_transaction_controller() {     
  }
  public class sw{
    public Cag_sale__c si {get;set;}
    public list <spdw> spdws {get;set;}
  }

  public class spdw{
    public CAG_Sale_to_pd_juction__c spdi {get;set;}
    public list <pdw> pdws {get;set;}
  }

  public class pdw{
    public Product_to_Device__c pdi {get;set;}
    public list <dw> dws {get;set;}

  }

  public class dw{
    public Cag_devices__c di {get;set;}
    public list <dgw> dgws {get;set;}
  }

  public class dgw{
    public CAG_Diagnosis__c dgi {get;set;}
  }

  public list <sw> sws {get;set;}

  public list <CAG_Transaction__c> gettlist(){
  	set<id> diagID = new set<id>();
  		for(CAG_Transaction__c ti: [select id from CAG_Transaction__c where id = :transid]){
  			t = [select id, cag_sale__c from CAG_Transaction__c where id = :transid];
  		}
  		s = [select id from cag_sale__c where id = :t.cag_sale__c];
  		list <cag_sale__c> sis =[select id, (select id from CAG_Sale_to_pd_juctions__r), (select CAG_Diagnosis__c from CAG_Sale_to_Diagnosis_Junction__r) from cag_sale__c where id = :t.cag_sale__c];    
	      for (cag_sale__c si : sis){
	        list <CAG_Sale_to_pd_juction__c> spdis = [select id, CAG_Product_to_Device__c from CAG_Sale_to_pd_juction__c where id = :si.CAG_Sale_to_pd_juctions__r];      
	        list<CAG_Sale_to_Diag__c> salediags = [select id,CAG_Diagnosis__c  from CAG_Sale_to_Diag__c where id = :si.CAG_Sale_to_Diagnosis_Junction__r];
	        for(CAG_Sale_to_Diag__c salediag : salediags){
	            diagID.add(salediag.CAG_Diagnosis__c);
	            }      
	        sw addsws = new sw();
	        sws.add(addsws);
	        addsws.si=si;
	        addsws.spdws = new list <spdw>();
	        for (CAG_Sale_to_pd_juction__c spdi : spdis){
	          list <Product_to_Device__c> pdis = [select id, CAG_Devices__c,Membership__r.Membership__c, Membership__r.Duration__c, Membership__r.price__c, upfront__r.upfront__c, Upfront__r.price__c, upfront_addition__c, membership_addition__c, Membership_Subtraction__c, Upfront_Subtraction__c, upfront_price__c, membership_price__c from Product_to_Device__c where id = :spdi.CAG_Product_to_Device__c];
	          spdw addspdws = new spdw();
	          addsws.spdws.add(addspdws);
	          addspdws.spdi = spdi;
	          addspdws.pdws = new list <pdw>();
	          for (Product_to_Device__c pdi : pdis){
	            list<Cag_devices__c> dis = [select id, Device_Type__c, Brand__c, Model__c, Operating_System__c, (select id from cag_diagnoses__r), Password__c, Username__c, no_username__c from Cag_devices__c where id = :pdi.CAG_Devices__c];
	            pdw addpdws = new pdw();
	            addspdws.pdws.add(addpdws);
	            addpdws.pdi = pdi;
	            addpdws.dws = new list <dw>();
	            for (Cag_devices__c di : dis){
	              list <CAG_Diagnosis__c> dgis = [select id, Why_did_the_customer_call_in__c, Main_Picklist__c, Additional_Description__c from CAG_Diagnosis__c where id = : di.cag_diagnoses__r and id in :diagID];
	              dw adddws = new dw();
	              addpdws.dws.add(adddws);
	              adddws.di = di;
	              adddws.dgws = new list <dgw>();
	              for (CAG_Diagnosis__c dgi :dgis){
	                dgw adddgws = new dgw();
	                adddws.dgws.add(adddgws);
	                adddgws.dgi = dgi;
	              }
	            }
	          }
	        }
	      }
      return [select id from CAG_Transaction__c where id = :transid];
  }

}

 

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!