8

This is for academic purposes

I am trying to run some code from visualforce in practise and came across this sample page and related controller

page is

<apex:page controller=”myController” >
<apex:form >
<apex:pageBlock title=”My Content” mode=”edit”>
<apex:pageBlockButtons >
<apex:commandButton action=”{!save}” value=”Save”/>
</apex:pageBlockButtons>
<apex:pageBlockSection title=”My Content Section”
columns=”2”>
<apex:outputLabel for=”aName”>Account Name:</
apex:outputLabel>
<apex:inputText value=”{!accountName}”/>
</apex:pageBlockSection>
</apex:pageBlock>
</apex:form>
</apex:page>

controller code is

public with sharing class myController {
private final Id accountId&nbsp;;
transient public final String accountName {get; set; }
public myController() {
Account account = [select Id, Name from Account where id
=:ApexPages.currentPage().getParameters().get(‘id’)];
accountId = account.Id&nbsp;;
accountName = account.Name&nbsp;;
}
public PageReference save() {
Account myAccount = [select name from Account where id
=:accountId];
myAccount.name = accountName;
update myAccount;
return null;
}
}

where i am confused is,

myAccount.name = accountName;

This code works like this:

Page gets pre populated with account name on load.User enters edits account name.Clicks Save.It gets saved to DB.

Now this is where i am confused:

We marked 'account name' variable as transient in controller.So how come value entered by user still persist in 'accountname' variable. When user clicks save,save method is executed during the post back request.since accountname is not view state i thought variable will not have the value entered by user

Maybe i don't understand view state properly. Can somebody clarify this please?

  • I think Save method is executed in the same context or ?? – ManSpan May 13 '14 at 15:14
  • as per post back execution order--action method is triggered separately?salesforce.com/us/developer/docs/pages/Content/… – sfdc99999 May 13 '14 at 15:38
  • I think the save method is the action which triggered the post back right ? So viewstate is updated right after the method – ManSpan May 13 '14 at 15:58
  • My confusion was how come save method at that point has the value entered by user.As its not in view state – sfdc99999 May 13 '14 at 16:04
  • well it's not in view state but it is in the same context so it's visible :) – ManSpan May 13 '14 at 16:27
16

Transient variables are not stored in the view state, but can be regenerated by the post back if bound to an input element. In general, you only need to use the view state for elements not bound to an input element and need to maintain state between post backs. The standard controller for a page, for example, should not be transient, because it won't stick. However, any element bound by input elements don't need to be saved in the view state if they are rendered.

In other words, the view state is first restored into memory (deserialized), and since accountName wasn't stored in the view state (it is transient), the initial value is null. Then, Visualforce calls set(accountName, value), because it's bound to a rendered input element, which effectively restores its state. Finally, save() is called and the value is correctly populated.

  • that was really helpful and new information..thanks :)(I did try with a transient output element and could see in DEBUG log value is not getting stored between states :))..thanks again – sfdc99999 May 13 '14 at 22:05
  • 1
    If it were not transient, it would have the value 'AccountA' until the setter were called, in which case 'AccountA' would be overwritten with 'AccountB'. If you were using a custom setter method, you could prove this: public string accountName { get; set { system.debug(accountName); accountName = value; } }. – sfdcfox May 14 '14 at 14:35
  • 2
    I don't have a link, but... It's stored in the form's parameters. There is no direct access to this area of memory, as it is managed by the Visualforce runtime engine. The "setter" stage of the Visualforce engine is when this value becomes assigned to the controller's memory (the heap) from the form parameters. Once it is part of the heap, it remains there for the rest of the transaction, unless you change the value. Once the Visualforce engine reaches the "getter" state, the value is re-encoded back into a form parameter via an input field. – sfdcfox May 15 '14 at 1:20
  • 1
    The value is not serialized into the view state when it is transient. – sfdcfox May 15 '14 at 1:20
  • 1
    Yes, if a variable isn't set at all, it usually won't appear in the view state until it is set. It's usually better to explicitly initialize any variables in the constructor so they will appear in the view state. Of course, transient variables should never appear in the view state. – sfdcfox May 17 '14 at 0:47

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.