+ Start a Discussion
Pulkit Sood 6Pulkit Sood 6 

Where should i consider to use view state or just transfer the Id and everytime i need to work on a record i bring values into transient variables

hi, I am new to salesforce, was given a dummy project as training purpose after reading about view state and execution order, context,etc. i am a bit confused about how to save the state(as with static vairables) through multiple request to the Server, i can be mistaking but that's what i got till now,

static vars can't be used because, HTTP, being stateless, removes them after every request in the VFpage

can use  view state but that would increase transmission lag, can use transient variables(to have one non-transient "id" variable and retreieve rest of the record fields), but that would require querying everytime a user makes database request,

can anyone tell me when to consider using the transient variable method and when view state?? Thanx

Transient keyword to declare instance variable that can not be saved and should not transmitted as part of view state for visual force page.
Basically ,View State is an encrypted, hidden <input> field on a Visualforce page that keeps track of Apex controller state & Visualforce page state between server requests. This field is only generated when there is an <apex:form> tag present on a page.

ViewState is only used on a single page that handles postbacks. Once you redirect to the new page, the ViewState is lost.

The Whole Process of Calling is like this :

A. URL Requested
B. Apex Controller Instantiated on Server
C. Controller State Serialized & Encrypted to View State
D. Page Markup Sent to Browser & Rendered
E. View State Decrypted & Deserialized (for Postbacks)

   View State Automatically keeps track of field values for you and Allows for easy AJAX functionality. In order to remove View State Error we use 4 methods.

A. Reduce Number of Components
B. Use the transient Keyword
C. Use JavaScript Remoting
D. Use the Streaming API

Declare only those fields as transient which values you dont need in visualforce page. 

i hope it helps you.
    Let me inform if it helps you and kindly mark it best answer if it helps you so it make proper solution for others
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. 
Pulkit Sood 12Pulkit Sood 12
Thanx man!! but my id has expired... so cant close question neither mark answer!! but thanx a lot anyways