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
Don PierceDon Pierce 

Problem displaying list from Apex Class in emailtemplate

I created a simple Apex Class and VFC to display my query results in an email template.
The only thing it displays is the correct size of the list and a blank POId . No List results.
*********************Apex Code
global class POComponent
{
global string POId
    {
    get;
    set;
    }
global integer leng
    {
    get;
    set;
    }
global List<rstk__poline__c> RSNPOLine2
    {
    get;
    set;
    }
public POComponent()
    {
    POComponentCore();
    }
public List<rstk__poline__c> POComponentCore()
    {
    POId = ApexPages.currentPage().getParameters().get('id');
    List<rstk__poline__c> RSNPOLine2  = new List<rstk__poline__c>();
    RSNPOLine2 = [SELECT rstk__poline_lne__c,rstk__poline_item__c, rstk__poline_descr__c FROM rstk__poline__c where rstk__poline_ordno__c = :POId];
     leng = RSNPOLine2.size();
    return RSNPOLine2;
    }
}
*************************VFC code:
<apex:component controller="POComponent" access="global">
<apex:attribute name="configproducts" description="DCPId" type="String" assignTo="{!POId}"/>
            <table>
            <apex:repeat var="arr" value="{!RSNPOLine2}">
                <tr>
                    <td style="width: 010mm;   height: 004mm;">Just a test inside VFC</td>
                    <td style="width: 025mm;   height: 004mm;">
                        <div style="width: 100%; max-width:100%; white-space: nowrap; overflow: hidden;">{!arr.rstk__poline_descr__c}</div>
                    </td>
                    <td style="width: 067mm; height: 004mm; ">
                        <div style="width: 100%; max-width:100%; white-space: nowrap; overflow: hidden;">{!arr.rstk__poline_item__c}</div>
                    </td>
                    <td style="width: 010mm;   height: 004mm;">
                        <div style="width: 100%; max-width:100%; white-space: nowrap; overflow: hidden; text-align: right;">{!arr.rstk__poline_lne__c}</div>
                    </td>
                    <td style="width: 010mm;   height: 004mm; ">
                        <div style="width: 100%; max-width:100%; white-space: nowrap; overflow: hidden; text-align: left; margin-left: 1mm;">CCCCC</div>
                    </td>
                    <td style="width: 025mm;   height: 004mm;">
                        <div style="width: 100%; max-width:100%; white-space: nowrap; overflow: hidden; text-align: right;">1111.99</div>
                    </td>
                </tr>
             </apex:repeat>
            </table>
                <table>
                <tr><td>This is outside the repeat loop, list size = {!leng} and POId is {!POId}</td></tr></table>
</apex:component>
******************Email Template code:
<messaging:emailTemplate subject="Purchase Order" recipientType="User" relatedToType="rstk__PO_Print_Header__c">
<messaging:htmlEmailBody >
<html>
<head>
<style type="text/css" media="print">
 @page {
     size:A4;
     margin-left: 0.8cm;
     margin-right: 0.8cm;
     margin-top: 12.0cm;
     margin-bottom: 4.5cm;
     @top-center {
           content: element(header);
       }
     @bottom-left {
           content: element(footer);
       }
}
 </style>
 </head>
 <body>
        <c:POComponent configproducts="{!relatedTo.Id}"/>
</body>
</html>
</messaging:htmlEmailBody>
</messaging:emailTemplate>
Best Answer chosen by Don Pierce
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Don, 

Please use this apex code, 
 
global class POComponent
{
	global string POId{get;set;}
	global integer leng{get;set;}
	public POComponent() 
	{
		// Empty Constructor
	}
	public List<rstk__poline__c> getRSNPOLine2()
    {
		POId = ApexPages.currentPage().getParameters().get('id');
		List<rstk__poline__c> RSNPOLine2  = new List<rstk__poline__c>();
		RSNPOLine2 = [SELECT rstk__poline_lne__c,rstk__poline_item__c, rstk__poline_descr__c FROM rstk__poline__c where rstk__poline_ordno__c = :POId];
		leng = RSNPOLine2.size();
		return RSNPOLine2;
    }
}

Explanation : for Visualforce component there is no as such use of constructor. Developer need to create get methods.

--PS

All Answers

Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Don, 

your relatedToType is rstk__PO_Print_Header__c and you are using the Id of that object. But you ar quering it from the rstk__poline__c object. This is the reason you are getting blank records. 

Thanks
Prosenjit
Don PierceDon Pierce
I went ahead and changed the emailtemplate first line too:
<messaging:emailTemplate subject="Purchase Order" recipientType="User" relatedToType="rstk__poline__c">

But still have the same problem.  The only thing that is displayed when rendered is shown below.
Both the POId and size are correct values so I know it is pulling the data I need(in apex class):
____
This is before the repeat loop, list size = 16 and POId is a8q0U0000004D2DQAU
This is outside the repeat loop, list size = 16 and POId is a8q0U0000004D2DQAU
____
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Don, 

Please use this apex code, 
 
global class POComponent
{
	global string POId{get;set;}
	global integer leng{get;set;}
	public POComponent() 
	{
		// Empty Constructor
	}
	public List<rstk__poline__c> getRSNPOLine2()
    {
		POId = ApexPages.currentPage().getParameters().get('id');
		List<rstk__poline__c> RSNPOLine2  = new List<rstk__poline__c>();
		RSNPOLine2 = [SELECT rstk__poline_lne__c,rstk__poline_item__c, rstk__poline_descr__c FROM rstk__poline__c where rstk__poline_ordno__c = :POId];
		leng = RSNPOLine2.size();
		return RSNPOLine2;
    }
}

Explanation : for Visualforce component there is no as such use of constructor. Developer need to create get methods.

--PS
This was selected as the best answer
Don PierceDon Pierce
Thanks for the code I tried to compile and I get the following error Compile Error: The property List RSNPOLine2 is referenced by Visualforce Component (POComponent) in salesforce.com. Remove the usage and try again. at line 19 column 30 Also this line public List getRSNPOLine2() is correct? It is not clear to me what this is getRSNPOLine2() When I change to public List RSNPOLine2 I get error about unexpected token POID Don Pierce
Prosenjit Sarkar 7Prosenjit Sarkar 7
Hi Don, 
Thats normal. Just comment the whole VF component and save. Next paste the whole apex code and try. It was happened because I changed the code. 

--PS
Prosenjit Sarkar 7Prosenjit Sarkar 7
And yes the method name should be getRSNPOLine2. Otherwise it will not work. this is a get method of RSNPOLine2 what you are using in Viusalforce page. Please follow these steps.
1. Comment the whole Visualforce component and save.
2. Paste the EXACT apex code that I have provided and save.
3. Uncomment the whole Visualforce Page and save.
4. Test your requirement. 
5. If it is okay mark my reply as Best Answer ;)
6. If it is not okay let me know 

--PS
Don PierceDon Pierce
Perfect thanks