4

I have a custom button which generates and sends an invoice pdf. Just before to generate the pdf, if the issue date of the invoice is null, I set the date to today, then I generate the PDF. The code looks something like :

Invoice_Header__c inv = [select id, Issue_Date__c from Invoice_Header__c where Id = 'a0TN0000000kurH'];
System.debug('$1-- Issue Date = '+inv.Issue_Date__c); // my issue date = null here
if (inv.Issue_Date__c == null)
{
    inv.Issue_Date__c = System.today();
    update inv;
}

//reload the invoice just in case I would have something weird in trigger/workflow
inv = [select id, Issue_Date__c from Invoice_Header__c where Id = 'a0TN0000000kurH'];
QUtils.debug('$2-- Issue Date = '+inv.Issue_Date__c);   //it displays the right date here ==> today
//then I call my pdf
PageReference pdf = new Pagereference('/apex/MyInvoicePDF?id='+inv.Id);
Blob b = pdf.getContent();
.....

My 'MyInvoicePDF' page is linked to a standard + custom controller

<apex:page standardcontroller="Invoice_Header__c" extensions="MyInvoicePDFCtrl" renderAs="pdf" sidebar="false" showHeader="false" applyHtmlTag="false">

In my custom controller, I display the Issue Date of my invoice, however the issue date is still null :

public MyInvoicePDFCtrl(Apexpages.StandardController controller)
{
    ...
    Invoice_Header__c invoice = [select id, Issue_Date__c from Invoice_Header__c where Id = 'a0TN0000000kurH'];
    System.debug('$3-- Issue Date = '+invoice.Issue_Date__c); //my Issue Date is still null here 
    ...
}

Is there anyone who got the same problem before ? Is it a timing/cache issue ?

Cheers

David

4
  • Can you paste the MyInvoicePDF here? Commented Dec 5, 2013 at 5:57
  • I updated my question. I tried with and without 'cache' on my apex:page
    – brovasi
    Commented Dec 5, 2013 at 6:08
  • Can you provide the code displaying the invoice.Issue_Date__c in pdf Commented Dec 5, 2013 at 6:18
  • I do not use invoice.Issue_Date__c directly in my pdf. it's a public variable because I have some logic around that. What I don't understand it's why it doesn't recognize my update when I call MyInvoicePDF, I use system debug in the constructor and invoice.Issue_Date__c is null. It doesn't make really sense.
    – brovasi
    Commented Dec 5, 2013 at 8:48

2 Answers 2

3

I have had this same sort of thing happen before. Rough description: The root cause had something to do with how transactions are handled behind the scenes and what has been committed to the DB for other transactions to see. In this situation, even though the update is done it is till not fully available in the DB. So, when code from a different controller/page tries to access it like is done with your call to the new PageReference, the value is still null.

The solution is to make sure that the generation of the PDF is done in a different transaction from the one where the update is done. There are different ways to achieve this, for example, after your update you can forward to a different blank page that has its page action attribute set to call a method that immediately generates the PDF.

2
  • 1
    You can also set the invoice date via a commandButton, then call an actionFunction 'oncomplete' for the additional PDF getContent logic. Saves creating another page.
    – Ben
    Commented Dec 5, 2013 at 15:44
  • Sure! Glad to help! Commented Dec 5, 2013 at 23:29
0

Change hard coded id to to the dynamic one as given below

public MyInvoicePDFCtrl(Apexpages.StandardController controller)
{
    ...
    Invoice_Header__c invoice = [select id, Issue_Date__c from Invoice_Header__c where Id = :Apexpages.currentPage().getParameters().get('id')];
    System.debug('$3-- Issue Date = '+invoice.Issue_Date__c); //my Issue Date is still null here 
    ...
}
1
  • Yeah I tried a lot of different things to be honest, and I always had the same result. But thank you
    – brovasi
    Commented Dec 5, 2013 at 22:57

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .