0

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

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();
}
  • thanks! I'll try this. It looks like it answers my question. Much appreciated. – Natalie Spatharakis Oct 24 '16 at 16:14
  • Ah -- new issue. The field 'Id_and_Version__c' is not writeable -- I want to keep it this way, because it is a formula value to always be the account's current ID and field value for card_version. So I guess what I need to make sure happens is that when I change the card version in my controller, it needs to update the Id_and_Version__c and set it in the VF Page... – Natalie Spatharakis Oct 24 '16 at 16:26
  • For the alternate method, what is myvfPage ? it's not defined... – Natalie Spatharakis Oct 24 '16 at 16:29
  • @NatalieSpatharakis It's whatever you named your page (the "api name" of the page). For example, if the page is at "/apex/generateqrcode", then the code would be Page.generateqrcode. – sfdcfox Oct 24 '16 at 16:36
  • Yeah, I actually figured that one out -- when I go this route, it seems to be endlessly loading and loading in the pop up window... – Natalie Spatharakis Oct 24 '16 at 16:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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