[SalesForce] Updating a get;set; value in controller, but it’s not updating in the VF Page

Background Info:
There is a QR field that generates a QR code image from a URL. The URL uses a custom field that is a string composed of account.Id+card_version. When I update card_version, you now have the ability for a new QR card with a new URL.

I have a visual force page that shows the account name and its QR code. I generate this VF Page with a Button intended to create a new QR code. So, the controller updates card_version__c to be incremented by 1. When you refresh your original browser page, it updates everything — the fields, the QR code…however, upon clicking the button, the vf page still displays the original card_version__c value, and thus, an incorrect QR code.

I tried updating the account after incrementing, and setting a get;set; variable to be the id+card_version from the updated account, but it seems like this is not updating the VF page.

TL;DR — Updating value in controller, not updating in VF page, but it is updating the record.

CONTROLLER:

public class generateNewQR {

    public String USDA{get;set;}
    public Account account;
    public List<Account> contracts;
    public String exp{get;set;}
    public String accIdUpdate{get;set;}

    public generateNewQR(ApexPages.StandardController controller){

        // which fields are being pulled from current record
        if(!test.isRunningTest()){
            controller.addFields(new String[] {'Name','ID','card_version__c','Id_and_Version__c'});
        }

        // new account -- set to current record
        account = new Account();
        account = (Account)controller.getRecord();
    }

    public void updateCardVersion(){
        account.card_version__c = account.card_version__c + 1;
        update account;
        accIdUpdate = account.Id_and_Version__c;
        System.debug('  Card Version: ' + account.Id_and_Version__c);
        update account;
    }

}

VISUALFORCE PAGE:

<apex:page renderAs="PDF" standardController="Account" action="{!updateCardVersion}" extensions="generateNewQR" sidebar="false" standardStylesheets="false" applyHtmlTag="false" applyBodyTag="false" showHeader="false">
<html>
    <head>
        <style>
            @page {
                size:54mm 86mm; /* Printing area of Zebra badge */
                margin:0;
                }

                .badgeContent {
                    margin:0 auto;
                    text-align:center;
                    margin-top:1mm;
                }

                tt {
                    font-family:sans-serif;
                    font-size:10px;
                }
        </style>
    </head>

    <apex:form >
        <div class="badgeContent">
            <apex:image url="{!$Resource.logo}" width="80%"/><br/>
            <tt>{!Account.name}</tt><br/>
            <tt>CID#: {!Account.Client_Number__c}</tt><br/>
            <tt>Adults: {!Account.Household_Adults__c}. Children: {!Account.Household_Children__c}. Elders: {!Account.Household_Elder__c}.</tt><br/>
            <tt>Expiration Day:{!exp}</tt><br/>
            <tt><b>{!USDA}</b></tt><br /><br />
            <apex:image url="http://api.qrserver.com/v1/create-qr-code/?data={!accIdUpdate}&size=150x150" width="55%"/>

        </div>
    </apex:form>
</html>
</apex:page>

Best Answer

Once you perform a DML on a record, the copy you have in memory and the copy that's in the database are out of sync. So, you need to query the data back in order to get the latest values:

update account;
account.Id_and_card_version__c = [select Id_and_Card_version__c from account 
                                 where id = :account.Id].Id_and_Card_version__c;

Alternatively, you could just reload the entire page:

public PageReference updateCardVersion() {
    account.card_version__c+=1;
    update account;
    PageReference ref = Page.myvfPage;
    ref.getParameters().putAll(ApexPages.currentPage().getParameters());
    ref.setRedirect(true); // Discard page state
    return ref;
}

In this case, it actually looks like you just need to "recalculate" the formula:

public PageReference updateCardVersion() {
    account.card_version__c+=1;
    update account;
    account.recalculateFormulas();
}
Related Topic