{"id":2874,"date":"2012-06-01T00:14:02","date_gmt":"2012-05-31T18:44:02","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=2874"},"modified":"2012-06-01T00:14:02","modified_gmt":"2012-05-31T18:44:02","slug":"introduction-to-view-state-in-visualforce","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/introduction-to-view-state-in-visualforce\/","title":{"rendered":"Introduction to View State in Visualforce"},"content":{"rendered":"<p style=\"text-align: justify;\">As we know that http protocol is stateless protocol. Unlike Other programming languages like Java and C# the variables, objects are persisted between different method and network calls. However in case of server client model, the server only stores the session related information of client, however it does not stores the temporary data being used by the clients. Sometime hidden field values, IDs and other information are needed by server for processing.<\/p>\n<p style=\"text-align: justify;\"><span style=\"text-decoration: underline;\">For example :<\/span> If client needs any data and is loaded by the server in first request, if error comes the server will need to retrieve data again for second request. Also it is possible that few data is created at client side also, so to persist the state of client View State is used in Visualforce.<\/p>\n<p style=\"text-align: justify;\">Before Summer 12, For every form tag the View State was created. However from Summer 12 release only one View State will be created per page. View State is encrypted and transferred to client with each response. View State cannot be viewed with tools like firebug however salesforce has <strong>&#8220;View State Inspector&#8221;<\/strong> to check the View State Data.<\/p>\n<p><strong>Types of Data Saved in View State:<\/strong><br \/>\nFollowing data are saved in Viewstate<\/p>\n<ol>\n<li>All public and private data members present in Standard, Custom and Controller extensions.<\/li>\n<li>Objects that are reachable from data members in Custom and Controller extensions.<\/li>\n<li>The component tree for that page that represents the page&#8217;s component structure and the associated state which are the values applied to those components.<\/li>\n<li>Small amount of data needed by visualforce for other general purposes.<\/li>\n<\/ol>\n<p style=\"text-align: justify;\"><strong>Transient Variable and Objects:<\/strong><br \/>\nAs Viewstate is transferred over http with every response, it is very necessary to control the size of View State. There are lots of situations where data is not needed after postback or not needed on Visualforce page then in that case we can make variable or object transient. The transient variables are not passed to view state and therefore not stored in View State.<!--more--><\/p>\n<p><strong>Enable View State Inspector:<\/strong><\/p>\n<ol>\n<li>View State Inspector will be only visible when developer mode is ON in User profile.<\/li>\n<li>There is option to show the View State in User&#8217;s Personal information from where View State inspector can be enabled or disabled.<\/li>\n<\/ol>\n<p><strong>Sample Walk-through of View State:<\/strong><br \/>\nLets take example to demonstrate the behavior of View State.<\/p>\n<p>Visualforce page :<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;apex:page Controller=&quot;ViewStateController&quot;&gt;\n&lt;apex:form &gt;\n&lt;apex:commandButton value=&quot;Click Me&quot; action=&quot;{!changeData}&quot;\/&gt;\n&lt;\/apex:form&gt;\n&lt;\/apex:page&gt;\n<\/pre>\n<p>Consider below Apex class :<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic with sharing class ViewStateController {\n\npublic Integer a {get;set;}\nprivate Integer b {get;set;}\nTransient Integer c {get;set;}\nprivate Integer d{get;set;}\npublic Opportunity opp {get;set;}\n\npublic ViewStateController()\n{\n    a = 20;\n    b = 30;\n    c = 40; \/\/It will not visible in ViewState\n    opp = new Opportunity(name='Test',StageName='Test');\n}\n\/\/Call this method in Postback\npublic void changeData()\n{\n    if(a == 20)\n    {\n        a = 2;\n    }\n    if(b == 30)\n    {\n        b = 3;\n    }\n    \/\/As c is declared as Transient, it will not persist the value in Postback\n    if(c == 40)\n    {\n        d = 4;\n    }\n    else{\n        d= 40;\/\/As C value not persisted, else block will execute\n    }\n}\n}\n<\/pre>\n<p><strong>View State before POST request:<\/strong><br \/>\nIn above example, In constructor i have initialized object of opportunity and variable a,b and c.<\/p>\n<ol>\n<li>As the variable &#8220;c&#8221; is declared as <strong>Transient<\/strong>, it will<strong> not be visible<\/strong> in View State.<\/li>\n<li>Variable &#8220;b&#8221; is private, that means it is not accessible in Visualforce page however it will be available in View State.<\/li>\n<li>variable &#8220;d&#8221; is not initialized in constructor and hence it is not visible in view state.<\/li>\n<li>The Opportunity object have only those fields which was initialized in code. It does not have other fields present in opportunity.<\/li>\n<\/ol>\n<figure id=\"attachment_2875\" aria-describedby=\"caption-attachment-2875\" style=\"width: 574px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/View-State-in-Salesforce-before-Postback.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\" wp-image-2875  \" title=\"View State in Salesforce before Postback\" alt=\"View State in Salesforce before Postback\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/View-State-in-Salesforce-before-Postback-1024x287.png?resize=574%2C161&#038;ssl=1\" width=\"574\" height=\"161\" \/><\/a><figcaption id=\"caption-attachment-2875\" class=\"wp-caption-text\">View State in Salesforce before Postback<\/figcaption><\/figure>\n<p><strong>View State After POST request:<\/strong><\/p>\n<ol>\n<li>As the Variable &#8220;c&#8221; was declared as Transient, it didn&#8217;t persist value.<\/li>\n<li>As per logic when value was assigned in private variable &#8220;d&#8221;, it started to display in view state inspector.<\/li>\n<\/ol>\n<figure id=\"attachment_2876\" aria-describedby=\"caption-attachment-2876\" style=\"width: 574px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/06\/View-State-in-Salesforce-After-Postback.png?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\" wp-image-2876   \" title=\"View State in Salesforce After Postback\" alt=\"View State in Salesforce After Postback\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/06\/View-State-in-Salesforce-After-Postback-1024x274.png?resize=574%2C154&#038;ssl=1\" width=\"574\" height=\"154\" \/><\/a><figcaption id=\"caption-attachment-2876\" class=\"wp-caption-text\">View State in Salesforce After Postback<\/figcaption><\/figure>\n<p><strong>Tips on reducing View State Size:<\/strong><\/p>\n<p>As we know that total size of ViewState can be 135KB (On date of writing this article), There may be chances that you will hit maximum allowed limit or improve performance of Visualforce page by reducing Viewstate size:<\/p>\n<ol>\n<li>Declare variables as Transient if possible.<\/li>\n<li>Declare variable as Static, as it is not saved in View State.<\/li>\n<li>If you want to manage your own state, instead of using &lt;apex:form&gt; use HTML &lt;form&gt; tag instead.<\/li>\n<li>Recreate state instead of saving in Viewstate. Means if you can use SOQL instead of saving in some object or List, use it.<\/li>\n<li>You can also use Web service call, Ajax Remoting to recreate state instead of saving in Object.<\/li>\n<\/ol>\n<p><a title=\"View State in Salesforce\" href=\"http:\/\/wiki.developerforce.com\/page\/An_Introduction_to_Visualforce_View_State\" rel=\"nofollow\">You can also read this article on Salesforce blog which explains ViewState Mechanism.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction to View State in Visualforce with example and code walk-through<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[20,9,18],"tags":[337,336],"class_list":["post-2874","post","type-post","status-publish","format-standard","hentry","category-apex","category-salesforce","category-visualforce","tag-apex","tag-visualforce"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2470,"url":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/salesforce-tutorial-create-simple-ajax-based-visualforce-page\/","url_meta":{"origin":2874,"position":0},"title":"Salesforce Tutorial &#8211; Create Simple Ajax based Visualforce page","author":"Jitendra","date":"October 17, 2011","format":false,"excerpt":"Salesforce Tutorial - Step by step tutorial to create AJAX based application in visualforce page with Apex class","rel":"","context":"In &quot;Web Technology&quot;","block_context":{"text":"Web Technology","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/"},"img":{"alt_text":"Simple AJAX demo in salesforce using visualforce","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/10\/Simple-AJAX-demo-in-salesforce-using-visualforce.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2681,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions-part-5\/","url_meta":{"origin":2874,"position":1},"title":"Salesforce Interview Questions &#8211; Part 5","author":"Jitendra","date":"January 24, 2012","format":false,"excerpt":"Basic concepts and Interview Questions of salesforce, Visualforce, Apex and SOQL","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":28,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-questions\/","url_meta":{"origin":2874,"position":2},"title":"Salesforce Interview Questions &#8211; Part 1","author":"Jitendra","date":"May 6, 2010","format":false,"excerpt":"Set of most often asked questions on the salesforce.com developement","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":5796,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/lightning-data-services-standard-controller-for-lightning-components\/","url_meta":{"origin":2874,"position":3},"title":"Lightning Data Service &#8211; Standard Controller for Lightning Components","author":"Jitendra","date":"July 22, 2017","format":false,"excerpt":"Best Practices for Salesforce Lightning Component. How Lightning Data Service can improve Lightning Component performance and solve inconsistent data problem without writing single line of Apex code. Demo source code, image and slides included.","rel":"","context":"In &quot;Apex&quot;","block_context":{"text":"Apex","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/apex\/"},"img":{"alt_text":"Salesforce Lightning Data Services","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/07\/Salesforce-Lightning-Data-Services.png?fit=1045%2C395&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/07\/Salesforce-Lightning-Data-Services.png?fit=1045%2C395&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/07\/Salesforce-Lightning-Data-Services.png?fit=1045%2C395&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/07\/Salesforce-Lightning-Data-Services.png?fit=1045%2C395&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":6222,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/use-visualforce-in-classic-and-lightning-experience\/","url_meta":{"origin":2874,"position":4},"title":"Use Visualforce in Classic and Lightning Experience","author":"Jitendra","date":"August 29, 2017","format":false,"excerpt":"How to design a Visualforce page so that It would be displayed properly in classic as well in Salesforce Lightning Experience","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"Same Visualforce in classic and Lightning","src":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/08\/Same-Visiualforce-in-classic-and-Lightning.gif?fit=924%2C572&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/08\/Same-Visiualforce-in-classic-and-Lightning.gif?fit=924%2C572&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/08\/Same-Visiualforce-in-classic-and-Lightning.gif?fit=924%2C572&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.jitendrazaa.com\/blog\/wp-content\/uploads\/2017\/08\/Same-Visiualforce-in-classic-and-Lightning.gif?fit=924%2C572&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3218,"url":"https:\/\/www.jitendrazaa.com\/blog\/salesforce\/salesforce-interview-question-part-15\/","url_meta":{"origin":2874,"position":5},"title":"Salesforce Interview Question \u2013 Part 15","author":"Jitendra","date":"May 24, 2013","format":false,"excerpt":"141 : User Wants to set the starting day in Calendar as \"Monday\" instead of \"Sunday\". How to get it done? Ans : Change the user locale to \"English ( United Kingdom ) \" in Personal information or User record. 142 : Why CSS is not working in PDF created\u2026","rel":"","context":"In &quot;Salesforce&quot;","block_context":{"text":"Salesforce","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/salesforce\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2874","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=2874"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/2874\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=2874"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=2874"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=2874"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}