Sunday 30 July 2017

Transient Keyword – View State – Visualforce Page – Salesforce

Transient keyword

Transient keyword to declare instance variable in apex class that can not be saved and not to be transmitted as part of view state for visulforce page.
This helps to reduce the view state of visualforce page.
The value of transient keyword is only transferred from apex controller to visualforce page but not will be the part of view state. Due to which , changed value of transient variable is not sent back from visualforce page to controller when a new request is made by click either button or link.
Mostly we use transient keyword in 'Read Only' case in visualforce page by which data doesn’t sent back to controller in further request.

Basically, View state is an encrypted hidden <input> field on visualforce page that keeps track of Apex controller state and visualforce state between servedr request. 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.

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

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

Here , We will read about 'Use the transient Keyword' and below is the example--

Click For Demo


Visualforce Page:

<apex:page controller="TransientController">
    Non-Transient-Time: {!time1} <br/><br/>
    Transient-Time: {!time2} <br/><br/>
    <apex:form >
        <apex:commandLink value="Refresh"/>
    </apex:form>
</apex:page>

Apex Class:

public class TransientController {
    DateTime t1;
    transient DateTime t2; //declare
     
    public String getTime1() {
    if (t1 == null)
     t1 = System.now();
     
    return '' + t1;
    }
     
    public String getTime2() {
    if (t2 == null)
     t2 = System.now();
     
    return '' + t2;
    }
}

In the  above example, we can see that clicking refresh button changing transient date Time2 to be updated every time because it is creating each time page is refreshed and the non-transient date continues to have its original value, which has not been serialized from the view state, so it remains same.

Now let's go in deep understanding, when user opens page that means user made his first request, at that time both time values are null and getter method of both variables checked for null and assigned the value of 'System.Now()' to the variables and values got displayed on the page. When page was displayed for the first time, In the view state , there was only the value of non-transient variable which in t1 and the transient is t2.
So, when user click on refresh, a new request is made. The getter methods checks again for null. Because the view state has the variable of non-transient value , and this variable is not null then system does not refresh latest value of "System.now()".
But for the transient variable, view state does not hold previous value, so getter method could not find any value and assume it null again then getter method assign latest value of "System.now()" to the variable. That is why the value of transient variable gets refreshed.

0 comments:

Post a Comment

If you have any doubts, please let me know.