You need to sign in to do that
Don't have an account?
Gyanendra Singh 8
Getter, Setter and Action methods
Below is the code taken from Visualforce Developer's guide:
VF Page-----------
<apex:page controller="myController" tabStyle="Account">
<apex:form>
<apex:pageBlock title="Congratulations {!$User.FirstName}">
You belong to Account Name: <apex:inputField value="{!account.name}"/>
<apex:commandButton action="{!save}" value="save"/>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller class-----------------
public class MyController {
private final Account account;
public MyController() {
account = [SELECT Id, Name, Site FROM Account
WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
}
public Account getAccount() {
return account;
}
public PageReference save() {
update account;
return null;
}
}
My question is how is the Update happening here When the save button is clicked?
What I understand is, account points to the account record whose id is specified in the URL and then if I enter some account name on <apex:inputField value="{!account.name}"/> how is the account being getting updated as the account still continues to point to the Account record whose Id is in URL but then how does it takes up the account name from inputField?
VF Page-----------
<apex:page controller="myController" tabStyle="Account">
<apex:form>
<apex:pageBlock title="Congratulations {!$User.FirstName}">
You belong to Account Name: <apex:inputField value="{!account.name}"/>
<apex:commandButton action="{!save}" value="save"/>
</apex:pageBlock>
</apex:form>
</apex:page>
Controller class-----------------
public class MyController {
private final Account account;
public MyController() {
account = [SELECT Id, Name, Site FROM Account
WHERE Id = :ApexPages.currentPage().getParameters().get('id')];
}
public Account getAccount() {
return account;
}
public PageReference save() {
update account;
return null;
}
}
My question is how is the Update happening here When the save button is clicked?
What I understand is, account points to the account record whose id is specified in the URL and then if I enter some account name on <apex:inputField value="{!account.name}"/> how is the account being getting updated as the account still continues to point to the Account record whose Id is in URL but then how does it takes up the account name from inputField?
* query isn't quite the right word, but it fits best. It is more pulled from the state variable that is passed to the save method on the button click.
https://developer.salesforce.com/page/An_Introduction_to_Visualforce_View_State
If you have any particular questions after reading this document, I can try to help.