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
jgaiottojgaiotto 

PageReference getContent and static variables

Hi all,

I just ran into an issue involving a static variable and a pageRef.getContent()

I have a VF Page with a custom controller that allows a user to create invoices. Then, he's got the list of invoices (more than 250 by month).
The user can check the invoices he wants, and click a button that will create a document in a Chatter Group. The document contains all the invoices in one PDF.

Since I can't pass the invoices IDs in the URL as parameteres (the max number of caracters is 4096), I use a static variable (a set<Id>).

Behind the scenes :

Let's call the controller which lists the invoices PageInvoicesController
PageInvoicesController will store in a static variable in a class (UtilClass) all the IDs of the invoices records.
Then it will call the page to build the PDF (let's say PagePDF and PagePDFController)

In its contructor, PagePDFController will seek out in the static variable the IDs, and build the PDF.
Problem : the static variable is empty.


Utils :
public class Utils {
    public static set<Id> setInvoice = new set<Id>();
    /* some other code */
}


PageInvoiceController :
public class PageInvoiceController {
    /* some other code */

  public void print(set<Id> factureSet) {
        UtilClass.setInvoice = factureSet;
        /* I tested out with UtilsClass.setInvoice.addAll(factureSet) but the same thing happened */

        PageReference pageRef = Page.PagePDF;

        String stringToBlob = Test.isRunningTest() ? 'test' : EncodingUtil.base64Encode(pageRef.getContent());
      
        /* Post insertion with EncodingUtil.base64Decode(stringToBlob); */
        /* ... */
    }
}

PagePDFController :
public class PagePDFController {
    /* some other code */
    public list<Facture__c> listFactures { get; set; }

    public PagePDFController() {
        listFactures = [SELECT Id /* and other fields */ FROM Facture__c WHERE Id IN : Utils.setInvoice];
    }
}

The Util.setInvoice is empty, so the result is an empty PDF... it looks like the constructor of PagePDFController is not called or something

What do I do wrong ? Does the pageRef.getContent() empty the static variable ? Or maybe it is not part of the scope of the static variable ?

Thank you very much for your help !

PS : "facture" is the french word for invoice. You may find it in the code above ;)
Peter_sfdcPeter_sfdc
I would guess that either the id's aren't making it into factureSet, or the way you're invoking the page isn't actually generating PDF. 

You don't show the code for the page or the binding of the list to an actual property in your controller, or how you're invoking the print() method. So I'm curious about that. 

You might also try actually invoking the VF page using Http/HttpRequest/HttpResponse classes and seeing if you can get something different from there. 

I did some testing, and it surprised me that we actually do invoke the controller on a page that is statically referenced using the Page.PageName syntax. But I guess that makes sense. 

However, I maybe you should try actually invoking the page via HTTP get as follows: 

        String salesforceHost = System.Url.getSalesforceBaseURL().toExternalForm();

        String url =  salesforceHost + Page.PdfPage.getURL();
       
        HttpRequest req = new HttpRequest();
       
        req.setMethod('GET');
        req.setEndpoint(url);
        //req.setHeader('Content-type', 'text/html');
        req.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());       

        Http http = new Http();
        HTTPResponse res = http.send(req);
        String resBody = res.getBody();
        System.debug('actual body: '+resBody);