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>
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 propertytransactions
'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.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