Activity Forums Salesforce® Discussions Transient variables in Salesforce?

  • Piyush

    Member
    January 17, 2018 at 7:45 am

    Hi Manpreet,

    Transient keyword to declare instance variables that can't be saved, and shouldn't be transmitted as part of the view state for a Visualforce 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.

     

    <apex:page controller="TransientController">
    T1: {!t1} <br/>
    T2: {!t2} <br/>
    <apex:form>
    <apex:commandLink value="refresh"/>
    </apex:form>
    </apex:page>

     

    public class TransientController {

    DateTime t1;
    transient DateTime t2;

    public String getT1() {
    if (t1 == null) t1 = System.now();
    return '' + t1;
    }

    public String getT2() {
    if (t2 == null) t2 = System.now();
    return '' + t2;
    }
    }

    Hope this will help you .

    • This reply was modified 6 years, 10 months ago by  Piyush.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos