Thursday 1 March 2018

[#Blogged]Transient Keyword in Salesforce

Declaring variables as transient reduces view state size. A common use case for the transient keyword is a field on a Visualforce page that is needed only for the duration of a page request, but should not be part of the page's view state and would use too many system resources to be recomputed many times during a request.
The transient variables are not passed to view state and therefore not stored in View State.

difference between transient instance variable and a static variable?

Static methods, variables, or initialization code are associated with a class and are only allowed in outer classes.
When you declare a method or variable as static, it's initialized only once when a class is loaded. All static member variables in a class are initialized before any object of the class is created.  Indeed they aren't transmitted as part of the view state for a Visualforce page.
Using static variables will cause only one instance of the variable to be loaded when the application loads and that variable will not go out of memory until the app is closed.  It holds information that is common to all instances on a class and It is shared between them instead of being created a new with each instance.
while
Transient keyword to declare instance variable that can not be saved and should not be transmitted as part of view state for visual force page.
Basically, View State is an encrypted, hidden 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 a 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


References: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_classes_keywords_transient.htm

No comments:

Post a Comment