1

I am trying to understand the concept of view state and hence was going through the official documentation at : https://developer.salesforce.com/page/An_Introduction_to_Visualforce_View_State. I have a few queries on the explanation provided there. For the below code , it is mentioned:

When this page is posted back, we will need to retrieve the record again, apply the new values and then update the database. This highlights the main benefit of view state - making the job of a developer easier by automatically maintaining state between postbacks.

What I don't understand is :

  1. By saying that state is maintained between postbacks, I understand that they are implying that the value of accountID is not lost even after POST call is made. But why is the value of accountName(declared as transient) also maintained once the page loads after making the POST(save) call.. Why is accountName value not lost once save method is called.

  2. View state makes the job of developer easier by auto maintaining state between postbacks. Is there any other way the developer can get the state except for viewstate and hence to say that job becomes easier, isn't that an understatement?

Markup

<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

public with sharing class myController {

    private final Id accountId ;
    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 ;
        accountName = account.Name ;
    }

    public PageReference save() {
        Account myAccount = [select name from Account where id = :accountId];
        myAccount.name = accountName ;
        update myAccount;
        return null;
    }

}
4
  • 2
    accountName is resent to the controller on the save button click as it is part of the form
    – cropredy
    Apr 11, 2017 at 17:53
  • yes due to setter it is resent. But then how is state of accountName lost(as it is transient). Isn't the state still getting maintained
    – starhunter
    Apr 11, 2017 at 17:59
  • 1
    I guess the question is why you have accountName as transient. This is not a typical use case; more for query results or file uploads
    – cropredy
    Apr 11, 2017 at 19:16
  • To be frank, i am also trying to understand the reason of making accountName as transient. What does the documentation try to prove, is what i am trying to understand.
    – starhunter
    Apr 12, 2017 at 13:06

0

You must log in to answer this question.

Browse other questions tagged .