You need to sign in to do that
Don't have an account?
How to Use GETTER & SETTER on standard objects
Hi,
I am little confused with getter and setter methods for standard fileds on standard objects.
for example: On Account Object, for field Type, how do i use getter and setter methods.
VF Code:
<apex:page standardcontroller="Account" extensions="customSearch">
<apex:form >
<apex:pageBlock title="Custom Search" mode="Edit">
<apex:pageblockSection title="Select Fields" columns="1" >
<apex:inputField value="{!Account.Type}"/>
<apex:inputField value="{!Account.Rating}"/><br/>
</apex:pageblockSection>
<apex:pageBlockButtons location="bottom">
<apex:commandButton value="Search" action="{!doSearch}" reRender="SearchResults" />
</apex:pageBlockButtons>
</apex:pageBlock>
As you can see,input field is {!Account.Type} gives us the picklist and values.
From here how do i pass the value back to controller.
My Controller:
public class customSearch {
public customSearch(ApexPages.StandardController controller) {
}
/* Public Account acc=new Account();
public Account getacc(){
return acc;
}*/
String Account_Type;
public String getAccount_Type(){
return Account_Type;
}
public string setAccount_Type(string Account_Type){
Account_Type=Account_Type;
return Account_Type;
}
public List<Account> results=new List<Account>();
public void doSearch(){
System.debug('account Type == '+Account_Type);
results=[Select Name, Type from Account where Type=:Account_Type ];
// return null;
}
public List<Account> getResults(){
return results;
}
}
As you can see, i am trying to filter records based on the Type value.
But, when i runa debug log, the "Account_Type " is NULL.
I am a little confused on how to set and get standard field values.
Can any one help me point in right direction.
Thanks for your time!
Sales4ce.
The value will be automatically updated in the Account record of the standard controller when the page is submitted.
In order to access the account record from the standard controller, you would have something like:
Account acc; public customSearch(ApexPages.StandardController controller) { acc=(Account) controller.getRecord(); } ... public void doSearch(){ System.debug('account Type == '+acc.Type); results=[Select Name, Type from Account where Type=:acc.type ]; }
All Answers
The value will be automatically updated in the Account record of the standard controller when the page is submitted.
In order to access the account record from the standard controller, you would have something like:
Account acc; public customSearch(ApexPages.StandardController controller) { acc=(Account) controller.getRecord(); } ... public void doSearch(){ System.debug('account Type == '+acc.Type); results=[Select Name, Type from Account where Type=:acc.type ]; }
Thanks Bob. It worked for me.
I did not know that i need to call the controller.getrecord() method in the contructor.
getting to learn a lot.
Thanks again for your time!
Sales4ce
Forgive me if I'm still confused.
Is this:
The same as this:
If so, is one better? If not, why not?
Why not just set the things equal? I'm not trying to be snarky or smart... here just wondering about the benefits complexity of the language.
THANKS... Bob
It isn't quite the same as the code you have posted. It equates to:
The former is preferred, as its less code and you don't need to invoke the functions to test them. If you need to do something different to a simple get/set (i.e. update another field). Though to be honest, if I need to do that I would normally create full methods in the class, as I find that too much code embedded in the declaration doesn't help readability.
Thanks, Bob. Appreciate the help. One more small point:
Account_Type is a variable. accessible in the Controller just like it is. Accessible in the VF Page as {!Account_Type} or thru getAccount_Type.
getAccount_Type is a function just like the old Fortran functions. It is not ever a variable as such.
Correct?
Thanks again...Bob
You can have a method named getAccount_Type() which is used by the page as {!account_type} as the platform automatically prepends the get to the property name that you specify.
If you define it as:
The platform automatically makes a getter/setter available to things like the visualforce page. However, you would just access the property from apex via its name.
Thanks, Bob. This helps a LOT!