Friday, June 1, 2012

Transient Variable in Salesforce


Transient Variable in Sales force:
        Transient keyword to declare instance variable that can not be saved and should not transmitted as part of view state for visual force page.
View state maintains state of the Database between request and visual force page.

     Given below example is use of transient variable where we have created two Date Time and populating. Refresh button is using for refresh the VF page however only time T2 will change at because of this is declare as transient.

VF page:
<apex:page controller="ExampleController">
Time1: {!t1} <br/><br/>
Time-Transient: {!t2} <br/><br/>
<apex:form >
<apex:commandLink value="Refresh"/>
</apex:form>
</apex:page>
Controller Class:
public class ExampleController {
DateTime t1;
transient DateTime t2; //declare
public String getT1() {
if (t1 == null) t1 = System.now();
return '' + t1;
}
public String getT2() {
if (t2 == null) t2 = System.now();
return '' + t2;
}
}

No comments:

Post a Comment