0

EDIT: Updated Apex controller

I want to pass custom object records to a vf page to be rendered as a table. See below code. The controller method is called from a LWC.

Apex Controller:

public List<Transaction__c> getTransactions() {
    String transactionIds = ApexPages.currentPage().getParameters().get('transactions');
    String formattedString = transactionIds.remove(')');
    String formattedString2 = transactionIds.remove('(');
    List<String> idList = formattedString2.split(',');
    List<Transaction__c> transList = [SELECT Id, Amount__c FROM Transaction__c
    WHERE Id IN :idList];
    return transList;
}

@AuraEnabled 
public static String getPdfFileAsBase64String(List<Id> transactionIds) {
    PageReference pdfPage = Page.TransactionStatement;
    pdfPage.getParameters().put('transactions', transactionIds.toString());
    Blob pdfBlob = pdfPage.getContent();
    String base64Pdf = EncodingUtil.base64Encode(pdfBlob);
    return base64Pdf;
}

VF Page:

<apex:page controller="PrintJobPDFController" renderAs="pdf" applyHtmlTag="false" showHeader="false" cache="true" readOnly="true">
   <apex:pageBlock title="Transactions">
      <apex:pageBlockTable value="{!$CurrentPage.parameters.transactions}" var="trans">
         <apex:column value="{!trans.Amount__c}"/>
      </apex:pageBlockTable>
   </apex:pageBlock>
</apex:page>

I'm sure I need to do something other than transactions.toString() in the controller but I'm not sure what. Currently I get an error Unknown property 'String.Amount__c'. I have tried with a VF page looking like this but I just get a blank table:

<apex:page controller="PrintJobPDFController" renderAs="pdf" applyHtmlTag="false" showHeader="false" cache="true" readOnly="true">
   <apex:pageBlock title="Transactions">
      <apex:pageBlockTable value="{!transactions}" var="trans">
         <apex:column value="{!trans.Amount__c}"/>
      </apex:pageBlockTable>
   </apex:pageBlock>
</apex:page>
4
  • The URL parameter transactions is a singleton String but you are trying to interpret it as a collection of SObjects in the VF page. You'll need the controller property transactions's getter to fetch the URL value and construct a list of sobjects for this to work. Don't fetch the URL param in the page.
    – cropredy
    Jun 14 at 16:12
  • Thanks, I now have public List<Transaction__c> getTransactions() { String transactionIds = ApexPages.currentPage().getParameters().get('transactions'); List<String> idList = transactionIds.split(','); List<Transaction__c> transList = [SELECT Id FROM Transaction__c WHERE Id IN :idList]; return transList; } but it still doesn't seem to be returning anything
    – Luke Bray
    Jun 14 at 17:23
  • please use edit and amend your code to what you now have; too hard to interpret code in comments.
    – cropredy
    Jun 14 at 17:37
  • thanks, have done this now.
    – Luke Bray
    Jun 15 at 8:42

1 Answer 1

1

Figured out that transactionIds had whitespace on each element so when split into List<String> each element looked like " a0O25000007doLmEAI". Once I removed the whitespace it worked.

PrintJobPDFController :

public with sharing class PrintJobPDFController {

    public List<Transaction__c> getTransactions() {
        String transactionIds = ApexPages.currentPage().getParameters().get('transactions');
        String formattedString = transactionIds.deleteWhiteSpace();
        List<String> idList = formattedString3.split(',');

        List<Transaction__c> transList = [SELECT Id, Amount__c FROM Transaction__c
        WHERE Id IN :idList];
        return transList;
    }

    @AuraEnabled 
    public static String getPdfFileAsBase64String(List<Id> transactionIds) {
        PageReference pdfPage = Page.TransactionStatement;
        pdfPage.getParameters().put('transactions', transactionIds.toString());
        Blob pdfBlob = pdfPage.getContent();
        String base64Pdf = EncodingUtil.base64Encode(pdfBlob);
        return base64Pdf;
    }
}

TransactionStatement.page:

<apex:page controller="PrintJobPDFController" renderAs="pdf" applyHtmlTag="false" showHeader="false" cache="true" readOnly="true">
   <apex:pageBlock title="Transactions">
      <apex:pageBlockTable value="{!transactions}" var="trans">
         <apex:column value="{!trans.Amount__c}"/>
      </apex:pageBlockTable>
   </apex:pageBlock>
</apex:page>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.