Take the 2-minute tour ×
Salesforce Stack Exchange is a question and answer site for Salesforce administrators, implementation experts, developers and anybody in-between. It's 100% free, no registration required.

I have one page that modifies a record but before the code finishes, it calls another page. The second page queries the values in the record but I'm assuming that my transaction from the update hasn't completed because the code hasn't finished. I am getting the content of the second page to add as an attachment. The problem is that I set a value on the record before generating the second page in memory. I'm then guessing that the transaction from the update hasn't completed then I build the second page in memory and it queries the field that I changed but gets the previous value because the transaction hadn't been fully committed.

Is there a way to force the commit before my code block exists so that the second page gets the correct value?

share|improve this question
    
There isn't, but there are workarounds. Take a look at Fields missing using getContent call for Visualforce rendered as PDF. –  Peter Knolle Dec 26 '13 at 20:09
    
if your page2 references a getter property in your controller, then that property can return the in-memory version of your record. –  crop1645 Dec 26 '13 at 20:11

2 Answers 2

No, you can't force a commit. The specific reason why is because commits occur at the end of the entire transaction. The data is committed after the DML, but isolated from the rest of the database until after the page finishes (so it can roll back in the event of an exception).

Shared View State

If the page shares the same controller and extensions as the current page, the view state should be transparently passed to the page you're calling getContent() on. This said, you don't need to query anything, because the data will already be present in the controller.

Multiple Calls

You can use an actionFunction to chain together multiple calls. You necessarily need to use a reRender attribute for this to work.

<apex:form>
    <!-- other intervening stuff here -->
    <apex:actionFunction action="{!sendEmail}" reRender="" name="sendEmail"/>
    <apex:commandButton action="{!updateRecord}" reRender="" value="Save & Send" oncomplete="sendEmail()"/> <!-- the last sendEmail is JavaScript! -->
share|improve this answer
    
Tried the static variable. It isn't working but it may be to complexity of the second page. There are multiple controllers for multiple sections. A Java programmer wrote them and abstracted the crap out of things so the page has more layers than an onion. –  Robert Harper Dec 26 '13 at 21:15
    
@RobertHarper I've had to do that myself, in which case it can definitely become painful. I am pretty sure the static method should work; I'll see if I can't draft up a working demonstration for you shortly. –  sfdcfox Dec 26 '13 at 21:43
    
I tried the static method but because of the way the second page is created, it gets a separate context and the static value returned null in the second page. –  Robert Harper Dec 26 '13 at 22:19
    
@RobertHarper It appears you're correct on that front. I could have sworn I had previously made something work that way, but no such luck here. I'll amend my answer. –  sfdcfox Dec 26 '13 at 22:21
up vote 1 down vote accepted

The way we fixed this was to add the page controller that held the state to the other pages so that the state could be persistent across the pages.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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