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
kathybbkathybb 

Problem rendering a dataTable in PDF page

I have a requirement to render a visualforce page in PDF and attach it to the relevant Purchase Order (custom Object).  Here is my visualforce code:

<apex:page standardController="SFDC_Purchase_Order__c" renderas="PDF" extensions="AttachPRTOPOExtension" sidebar="false" showHeader="false" name="PDFPO">
    <br />
    <br />
    <center><big><big><br /> Broadcast Interactive Media</big></big></center><br/>
    <apex:outputPanel id="POInfo" layout="Block" style="font:Arial font-size: 9pt">
    <apex:outputtext value="Purchase Order Number:  " />
        <apex:outputField value="{!SFDC_Purchase_Order__c.Name}"/>
        <br />
        <apex:outputtext value="Date:  " />
        <apex:outputField value="{!SFDC_Purchase_Order__c.PO_Date__c}"/><br/>
        <apex:outputtext value="Purchase Order Status:  " />
        <apex:outputField value="{!SFDC_Purchase_Order__c.Status__c}"/><br/>
        <br />
        <apex:outputtext value="Deliver To:  " />
        <apex:outputField value="{!SFDC_Purchase_Order__c.Ship_To_Location__c}"/>
        
        <br />
        <br />
    </apex:outputPanel>
      
    <apex:dataTable value="{!m_displayResultList}" var="r" border="1px" cellpadding="2px" cellspacing="2px" width="100%" style="font:Arial fontsize=7pt">
        <apex:column width="10%" headerValue="Req #" value="{!r.name}" />
        <apex:column width="10%" headerValue="Requested For" value="{!r.createdby.name}"/>
        <apex:column width="10%" headerValue="Date" value="{!r.Request_Date__c}"/>
        <apex:column width="10%" headerValue="Quantity" value="{!r.Quantity__c}" />
        <apex:column width="10%" headerValue="Item Requested" value="{!r.item_Requested__r.name}" />
        <apex:column width="10%" headerValue="Price" value="{!r.item_requested__r.Price_per_unit__c}" />
        <apex:column width="10%" headerValue="Ext. Price" value="{!r.Extended_Price__c}" />
        <apex:column width="10%" headerValue="Needed by" value="{!r.Receive_by_Date__c}" />
    </apex:dataTable>
    
    
</apex:page>

 And here are the relevant method from my controller extension:

public PageReference ViewPDF()
    {   
        system.debug('*************************START VIEWPDF******************************');
        system.debug('m_currentId = '+m_currentId);
        List<SFDC_Purchase_Requisition__c> m_reqResultList = new List<SFDC_Purchase_Requisition__c>();
        List<SFDC_Purchase_Requisition__c> m_displayResultList = new List<SFDC_Purchase_Requisition__c>();
        PageReference retPDF = new PageReference('/apex/PDFPO?id='+m_currentId);
        system.debug('m_myPO = '+ m_myPO);
        string PDFqueryPO = 'select Name, CreatedBy.name, Ship_To_Location__c,Expedite__c,PO_Date__c, Owner.id, Total_Price__c, PO_Notes__c from SFDC_Purchase_Order__c where Id = :m_currentId Limit 1';
        
        SFDC_Purchase_Order__c PDFPO = new SFDC_Purchase_Order__c();
        PDFPO =(SFDC_Purchase_Order__c)database.query(PDFqueryPO);
        string PDFReqResultListquery = m_RequestQueryByPOid;
        m_reqResultList= database.query(PDFReqResultListquery);
        for(SFDC_Purchase_Requisition__c thisReq : m_reqResultList){
            system.debug('thisReq.name = '+ thisReq.name);
            system.debug('thisReq.createdby.name = '+ thisReq.createdby.name);
                        system.debug('thisReq.Request_Date__c) = '+ thisReq.Request_Date__c);
                        system.debug('thisReq.quantity__c = '+ thisReq.quantity__c);
                        system.debug('thisReq.item_requested__r.name = '+ thisReq.item_requested__r.name);
                        system.debug('thisReq.item_requested__r.price_per_unit__c = '+ thisReq.item_requested__r.price_per_unit__c);
            system.debug('thisReq.extended_price__c = '+ thisReq.extended_price__c);
            system.debug('thisReq.receive_by_date__c = '+ thisReq.receive_by_date__c);
        m_displayResultList = new List<SFDC_Purchase_Requisition__c>();
        For(SFDC_Purchase_Requisition__c x : m_reqResultList)
        {
            if(x != null){
            x = (SFDC_Purchase_Requisition__c)x;
            m_displayResultList.add(x);
            system.debug('x='+x);
            system.debug(m_displayResultList.size());
            }
        }
        }
        m_PDFPage = attach();
        system.debug('about to return m_PDFPage from ViewPDF = '+ m_PDFPage);
        return m_PDFPage;
    }
      
    public PageReference attach() 
    {
        system.debug('********************************START ATTACH**********************');
         system.debug('m_currentId = '+m_currentId);
        Attachment myAttach = new Attachment();
         system.debug('m_currentId = '+m_currentId);
        myAttach.ParentId = m_currentId;    //Id of the object to which the page is attached
        if(m_myPO != null)
        {
            string myFileName = (m_myPO.name + ' - '+ date.TODAY() + '.PDF');
            myAttach.name = myFileName;
            myAttach.description = 'BIM Purchase Order Summary for '+ m_myPO.name +' Created on '+ date.Today() +' by '+ m_myPO.CreatedBy.name;
            PageReference myPdf = new PageReference('/apex/PDFPO?id=' + m_currentID);
                myAttach.body = myPdf.getContentAsPdf();
            try 
            {
                database.SaveResult sra = database.insert(myAttach);
                if(sra.issuccess() == true)
                {
                m_currentAttachId = sra.getID();
                }
                else
                {
                    // Operation failed, so get all errors                
                    for(Database.Error err : sra.getErrors()) 
                    {
                        System.debug('The following error has occurred.');                    
                        System.debug(err.getStatusCode() + ': ' + err.getMessage());
                        System.debug('Attachment fields that affected this error: ' + err.getFields());
                    } 
                } 
            } catch(Exception e){
                system.debug('Creation of PDF Failed');
                system.debug('Message was: '+ e);
        }
    }
        system.debug('about to return m_PDFPage from attach = '+ m_PDFPage);
        return m_PDFPage;
    }

 The debug statements confirm that the list is populated correctly, but when the page renders, it displays properly down to the header row on the table and is blank for the rest of the table.  I would be very grateful if someone could point out what I am doing wrong.  The attach method works perfectly, but the table data is missing every time.

 

Thanks in advance for any insight.

 

kathybb

Best Answer chosen by Admin (Salesforce Developers) 
kathybbkathybb

I finally found out that my PDF page was calling a new instance of the controller when it needed it's own controller.  This made it lose all of the Requisition records  and only display the PO information.  Thanks to Brian Conlon, who helped me off the board.