How to force a commit?

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?

Answer

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! -->

Attribution
Source : Link , Question Author : Robert Harper , Answer Author : sfdcfox

Leave a Comment