0

I have an array of Ids in a LWC that I need to pass to Apex. Here is my LWC function:

import downloadPDF from '@salesforce/apex/PrintJobPDFController.getPdfFileAsBase64String';
import downloadjs from '@salesforce/resourceUrl/downloadjs';

@api allTransactionRecords = []; //POPULATED ELSEWHERE BUT NOT RELEVANT FOR THIS QUESTION

generatePdf(){
        var transactionIds = [];
        
        for(let i = 0; i < this.allTransactionRecords.length; i++) {
            let obj = this.allTransactionRecords[i];
            transactionIds.push(obj.Id);
        }

        downloadPDF({transactionIds: transactionIds}).then(response => {
            let title = 'pdfTitle.pdf';
            var strFile = "data:application/pdf;base64," + response;
            window.download(strFile, title, "application/pdf");

        }).catch(error => {
            console.log('Error: ' + error.body.message);
        });
    }

transactionIds can in theory be an infinite list but realistically could be anywhere from 0 to 1000 ids.

Here is the Apex method getPdfFileAsBase64String that is called:

public static String getPdfFileAsBase64String(List<Id> transactionIds) {
        PageReference pdfPage = Page.TransactionStatement;
        pdfPage.getParameters().put('transactions', transactionIds.toString());
        Blob pdfBlob;
        if(Test.isRunningTest()) { 
            pdfBlob = blob.valueOf('Unit.Test');
          } else {
            pdfBlob = pdfPage.getContent();
          }
        String base64Pdf = EncodingUtil.base64Encode(pdfBlob);
        return base64Pdf;
    }

the purpose of this is so that a PDF can be generated using VF.

The VF calls the following method:

public List<Transaction__c> getTransactions() {
        String transactionIds = ApexPages.currentPage().getParameters().get('transactions');
        transactionIds = transactionIds.deleteWhiteSpace();
        transactionIds = transactionIds.replaceAll('[()]', '');
        List<String> IdList = transactionIds.split(',');

        List<Transaction__c> transList = [SELECT Id, Amount__c, Balance__c, Transaction_Type_Description__c, Transaction_Date__c FROM Transaction__c WHERE Id IN :idList ORDER BY Transaction_Date__c DESC];
        return transList;
    }

The problem is that transactionIds passed from the LWC to Apex always seems to be truncated as shown below:

(a0O25000008jv2yEAA, a0O25000008jv2zEAA, a0O25000008jv30EAA, a0O25000008jv31EAA, a0O25000008jv32EAA, a0O25000008jv34EAA, a0O25000008jv35EAA, a0O25000008jv36EAA, a0O25000008jv37EAA, a0O25000008jv38EAA, ...

I know that system.debug() will truncate strings in the logs however the amount that have been truncated equates to the amount of records that are displayed when my VF generates the PDF.

Is it something to do with how I am converting the a string here:

pdfPage.getParameters().put('transactions', transactionIds.toString());
3
  • 1
    You should pass a comma separated values for the transactions parameter from LWC instead of a array of string, URL query parameter is quite strict in general.
    – Raul
    Aug 2 at 11:06
  • thanks. I just got around it by querying the parent object of Transaction__c within the controller instead. That way I only had to pass 1 id from the LWC to Apex.
    – Luke Bray
    Aug 2 at 15:42
  • Sounds like a good and optimal solution! :)
    – Raul
    Aug 2 at 22:10

1 Answer 1

3

I discovered that the issue was coming from a character limitation on pdfPage.getParameters() (seemed to be limited at 205 characters for me)

I got around it by querying using the id of parent object of Transaction__c within the controller instead. That way I only had to pass 1 id from the LWC to Apex

List<Transaction__c> transList = [SELECT Id, Amount__c, Balance__c, Transaction_Type_Description__c, Transaction_Date__c FROM Transaction__c WHERE ParentRecordId =: parentRecordId ORDER BY Transaction_Date__c DESC];

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.