+ Start a Discussion
Jason Kuzmak 12Jason Kuzmak 12 

Why is my VisualForce reference variable working at all?

I have a problem. Or rather, I have a solution that I was expecting to be a problem, and that's bad! 
In the code below, after following some tutorial steps for creating a controller extension, I found that I was able to successfully reference a quote from Salesforce CPQ by binding expressions to a variable named "quote". However, I never named a variable "quote" in any of my code, my controller is pointing to a custom object called "EAM_Form__c", and Salesforce CPQ's custom quote object is called SBQQ__Quote__c, rather than just quote. So, I don't understand why I'm able to successfully reference a variable that I never named, for example "{!quote.Voltage__c}".  

A snippet of my page:
 

<apex:page standardController="EAM_Form__c" extensions="EAMFormExtension" showHeader="true">            
    <apex:form>
        <apex:pageBlock title="Application Analysis">                
            <apex:pageBlockSection columns="1">
                <apex:inputField value="{!quote.Voltage__c}"/>
                <apex:inputField value="{!quote.Phase__c}"/>
                <apex:inputField value="{!quote.Conveyor_Height__c}"/>
            </apex:pageBlockSection>
        </apex:pageBlock>
    <apex:form>
</apex:page>

My controller extension:
public class EAMFormExtension {
    private final EAM_Form__c myForm;

    // The extension constructor initializes the private member
    // variable mysObject by using the getRecord method from the standard
    // controller.
    public EAMFormExtension(ApexPages.StandardController stdController) {
        this.myForm = (EAM_Form__c)stdController.getRecord();
    }

    public SBQQ__Quote__c getQuote() {
        SBQQ__Quote__c myQuote = [Select Id, Name, Voltage__c, Phase__c, Conveyor_Height__c,SBQQ__Opportunity2__c From SBQQ__Quote__c Where Id = :this.myForm.Quote__c Limit 1];
        return myQuote;
    }
}

For the sake of learning how this works, can anyone explain this mysterious reference name to me?
Best Answer chosen by Jason Kuzmak 12
Tuan LuTuan Lu
myQuote is internal to the getQuote method so you can not access that from VF. The getter property is defined by the method name getQuote so in your VF page you can reference it as "quote".

All Answers

Tuan LuTuan Lu
Hi Jason you are using whats called a getter method. Here is a pretty good explanation for them below.

http://www.forcetree.com/2009/07/getter-and-setter-methods-what-are-they.html
 
Jason Kuzmak 12Jason Kuzmak 12

Hi Tuan,

Thanks, this is very informative! I am noticing that in the examples given, they specifically define the variable name "userinput" as a null string before writing the get method, and they use this name in their VisualForce code.

In my example, the only name I apply is "myQuote", yet this does not become the reference name in the visualforce page. I do not instantiate a quote object named "Quote", so I don't know how it knows to pull that name. Do you know where it could be getting this from?

Tuan LuTuan Lu
myQuote is internal to the getQuote method so you can not access that from VF. The getter property is defined by the method name getQuote so in your VF page you can reference it as "quote".
This was selected as the best answer
Jason Kuzmak 12Jason Kuzmak 12
Ok great, that's interesting. So it's considering everything beyond the word "get" as a reference-able variable name. Hence, I was able to do this as well:

in apex
public String yeah;

public String getThisThing(){
     yeah = 'woohoo';
     return yeah;
}

in VF
<apex:page standardController="EAM_Form__c" extensions="EAMFormExtension" showHeader="true">
   {!thisthing}
<apex: page/>

​in live

User-added image


Just starting into VisualForce, so this is very helpful to know. Thanks!