Maintain state in Visualforce Page without using View State
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty{ margin-bottom:0;
}
up vote
5
down vote
favorite
I would know if there is some way to maintain state across different transactions without using View State.
For example, if in my Custom Controller, for every action method, I need 100 Account's records, right now, I can follow two ways:
- Use static/transient keyword on this list. In this way, the View State doesn't contain my Account's records but I have to execute a SOQL every time I go on the server-side.
- Don't use static/transient keyword on this list. In this way, I have not to execute a SOQL every time I go on the server-side but I could have a problem with the View State.
Is there any way to store and keep information across different transaction not using the View State?
The only idea I got is using Platform Cache. Is it correct? Other solution available?
Thanks!
visualforce controller viewstate static platform-cache
add a comment |
up vote
5
down vote
favorite
I would know if there is some way to maintain state across different transactions without using View State.
For example, if in my Custom Controller, for every action method, I need 100 Account's records, right now, I can follow two ways:
- Use static/transient keyword on this list. In this way, the View State doesn't contain my Account's records but I have to execute a SOQL every time I go on the server-side.
- Don't use static/transient keyword on this list. In this way, I have not to execute a SOQL every time I go on the server-side but I could have a problem with the View State.
Is there any way to store and keep information across different transaction not using the View State?
The only idea I got is using Platform Cache. Is it correct? Other solution available?
Thanks!
visualforce controller viewstate static platform-cache
add a comment |
up vote
5
down vote
favorite
up vote
5
down vote
favorite
I would know if there is some way to maintain state across different transactions without using View State.
For example, if in my Custom Controller, for every action method, I need 100 Account's records, right now, I can follow two ways:
- Use static/transient keyword on this list. In this way, the View State doesn't contain my Account's records but I have to execute a SOQL every time I go on the server-side.
- Don't use static/transient keyword on this list. In this way, I have not to execute a SOQL every time I go on the server-side but I could have a problem with the View State.
Is there any way to store and keep information across different transaction not using the View State?
The only idea I got is using Platform Cache. Is it correct? Other solution available?
Thanks!
visualforce controller viewstate static platform-cache
I would know if there is some way to maintain state across different transactions without using View State.
For example, if in my Custom Controller, for every action method, I need 100 Account's records, right now, I can follow two ways:
- Use static/transient keyword on this list. In this way, the View State doesn't contain my Account's records but I have to execute a SOQL every time I go on the server-side.
- Don't use static/transient keyword on this list. In this way, I have not to execute a SOQL every time I go on the server-side but I could have a problem with the View State.
Is there any way to store and keep information across different transaction not using the View State?
The only idea I got is using Platform Cache. Is it correct? Other solution available?
Thanks!
visualforce controller viewstate static platform-cache
visualforce controller viewstate static platform-cache
asked Nov 12 at 15:51
vt89
18811
18811
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
up vote
6
down vote
The exact solution depends on the situation. You shouldn't think solutions as "either-or," as there's often an opportunity for hybrid solutions. There's also a few bonus solutions you can use depending on circumstances.
For example, you might choose to store just the record ID values in a list, and make a transient list for the records. Yes, you'll have to query the database each transaction, but you're trading some state for some performance.
Or, you might choose to use apex:inputHidden
and bind the record variables to these inputs. Variables bound to input fields can be transient. So, conceptually, you should be able to do this:
<apex:repeat value="{!accounts}" var="rec">
<apex:inputHidden value="{!rec.Id}" /><apex:inputHidden value="{!rec.Name}" />
</apex:repeat>
I haven't tried this model before, but I've used it similarly on Boolean values, String values, etc with success to reduce view state. You could even JSON serialize the data and put it in an apex:inputHidden.
This has a much larger limit, about 15,000,000 bytes of data (including serialization), allowing you to store relatively large amounts of data without any view state at all.
Platform Cache is nice, but you have to be prepared to do a query anyways, as it can expire at any time (the cache is not guaranteed to survive through its TTL).
In other words, there are solutions that you can use aside from pure view state or pure query methods.
2
+1 from me, love the JSON serialize idea!
– codeyinthecloud
Nov 12 at 16:16
add a comment |
up vote
1
down vote
When Can I Use Platform Cache?
You can use Platform Cache in your code almost anywhere you access the same data over and over. Using cached data improves the performance of your app and is faster than performing SOQL queries repetitively, making multiple API calls, or computing complex calculations.
The best data to cache is:
- Reused throughout a session, or reused across all users and requests
- Static (not rapidly changing)
- Expensive to compute or retrieve
Store Data That Doesn’t Change Often
Use the cache to store static data or data that doesn’t change often. This data is initially retrieved through API calls from a third party or locally through SOQL queries. If the data changes, cache this data if the values don’t have to be highly accurate at all times.
Store Data Obtained from Complex Calculations
Values that are a result of complex calculations or long queries are good candidates for cache storage. Examples of such data are:
- Total sales over the past week
- Total volunteering hours company employees did as a whole
- Top sales ranking
For clues on where to use Platform Cache, inspect your code. For example, do you currently store app data by overloading a Visualforce view state? These stored values are all candidates for Platform Cache.
Not every use case is a Platform Cache use case. For example, data that changes often and that is real-time, such as stock quotes, isn’t a good candidate for caching. Also, ensure you familiarize yourself with Platform Cache limitations. For example, if your data is accessed by asynchronous Apex, it can’t be stored in a cache that is based on the user’s session.
Keep in mind these considerations when working with Platform Cache.
- Cache isn’t persisted. There’s no guarantee against data loss.
- Some or all cache is invalidated when you modify an Apex class in your org.
- Data in the cache isn’t encrypted.
- Org cache supports concurrent reads and writes across multiple simultaneous Apex transactions. For example, a transaction updates the key PetName with the value Fido. At the same time, another transaction updates the same key with the value Felix. Both writes succeed, but one of the two values is chosen arbitrarily as the winner, and later transactions read that one value. However, this arbitrary choice is per key rather than per transaction. For example, suppose one transaction writes PetType="Cat" and PetName="Felix". Then, at the same moment, another transaction writes PetType="Dog" and PetName="Fido". In this case, the PetType winning value could be from the first transaction, and the PetName winning value could be from the second transaction. Subsequent get() calls on those keys would return PetType="Cat" and PetName="Fido".
- Cache misses can happen. We recommend constructing your code to consider a case where previously cached items aren’t found. Alternatively, use the CacheBuilder Interface, which checks for cache misses.
- Session cache doesn’t support asynchronous Apex. For example, you can’t use future methods or batch Apex with session cache.
- Partitions must adhere to the limits within Salesforce.
- The session cache can store values up to eight hours. The org cache can store values up to 48 hours.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
The exact solution depends on the situation. You shouldn't think solutions as "either-or," as there's often an opportunity for hybrid solutions. There's also a few bonus solutions you can use depending on circumstances.
For example, you might choose to store just the record ID values in a list, and make a transient list for the records. Yes, you'll have to query the database each transaction, but you're trading some state for some performance.
Or, you might choose to use apex:inputHidden
and bind the record variables to these inputs. Variables bound to input fields can be transient. So, conceptually, you should be able to do this:
<apex:repeat value="{!accounts}" var="rec">
<apex:inputHidden value="{!rec.Id}" /><apex:inputHidden value="{!rec.Name}" />
</apex:repeat>
I haven't tried this model before, but I've used it similarly on Boolean values, String values, etc with success to reduce view state. You could even JSON serialize the data and put it in an apex:inputHidden.
This has a much larger limit, about 15,000,000 bytes of data (including serialization), allowing you to store relatively large amounts of data without any view state at all.
Platform Cache is nice, but you have to be prepared to do a query anyways, as it can expire at any time (the cache is not guaranteed to survive through its TTL).
In other words, there are solutions that you can use aside from pure view state or pure query methods.
2
+1 from me, love the JSON serialize idea!
– codeyinthecloud
Nov 12 at 16:16
add a comment |
up vote
6
down vote
The exact solution depends on the situation. You shouldn't think solutions as "either-or," as there's often an opportunity for hybrid solutions. There's also a few bonus solutions you can use depending on circumstances.
For example, you might choose to store just the record ID values in a list, and make a transient list for the records. Yes, you'll have to query the database each transaction, but you're trading some state for some performance.
Or, you might choose to use apex:inputHidden
and bind the record variables to these inputs. Variables bound to input fields can be transient. So, conceptually, you should be able to do this:
<apex:repeat value="{!accounts}" var="rec">
<apex:inputHidden value="{!rec.Id}" /><apex:inputHidden value="{!rec.Name}" />
</apex:repeat>
I haven't tried this model before, but I've used it similarly on Boolean values, String values, etc with success to reduce view state. You could even JSON serialize the data and put it in an apex:inputHidden.
This has a much larger limit, about 15,000,000 bytes of data (including serialization), allowing you to store relatively large amounts of data without any view state at all.
Platform Cache is nice, but you have to be prepared to do a query anyways, as it can expire at any time (the cache is not guaranteed to survive through its TTL).
In other words, there are solutions that you can use aside from pure view state or pure query methods.
2
+1 from me, love the JSON serialize idea!
– codeyinthecloud
Nov 12 at 16:16
add a comment |
up vote
6
down vote
up vote
6
down vote
The exact solution depends on the situation. You shouldn't think solutions as "either-or," as there's often an opportunity for hybrid solutions. There's also a few bonus solutions you can use depending on circumstances.
For example, you might choose to store just the record ID values in a list, and make a transient list for the records. Yes, you'll have to query the database each transaction, but you're trading some state for some performance.
Or, you might choose to use apex:inputHidden
and bind the record variables to these inputs. Variables bound to input fields can be transient. So, conceptually, you should be able to do this:
<apex:repeat value="{!accounts}" var="rec">
<apex:inputHidden value="{!rec.Id}" /><apex:inputHidden value="{!rec.Name}" />
</apex:repeat>
I haven't tried this model before, but I've used it similarly on Boolean values, String values, etc with success to reduce view state. You could even JSON serialize the data and put it in an apex:inputHidden.
This has a much larger limit, about 15,000,000 bytes of data (including serialization), allowing you to store relatively large amounts of data without any view state at all.
Platform Cache is nice, but you have to be prepared to do a query anyways, as it can expire at any time (the cache is not guaranteed to survive through its TTL).
In other words, there are solutions that you can use aside from pure view state or pure query methods.
The exact solution depends on the situation. You shouldn't think solutions as "either-or," as there's often an opportunity for hybrid solutions. There's also a few bonus solutions you can use depending on circumstances.
For example, you might choose to store just the record ID values in a list, and make a transient list for the records. Yes, you'll have to query the database each transaction, but you're trading some state for some performance.
Or, you might choose to use apex:inputHidden
and bind the record variables to these inputs. Variables bound to input fields can be transient. So, conceptually, you should be able to do this:
<apex:repeat value="{!accounts}" var="rec">
<apex:inputHidden value="{!rec.Id}" /><apex:inputHidden value="{!rec.Name}" />
</apex:repeat>
I haven't tried this model before, but I've used it similarly on Boolean values, String values, etc with success to reduce view state. You could even JSON serialize the data and put it in an apex:inputHidden.
This has a much larger limit, about 15,000,000 bytes of data (including serialization), allowing you to store relatively large amounts of data without any view state at all.
Platform Cache is nice, but you have to be prepared to do a query anyways, as it can expire at any time (the cache is not guaranteed to survive through its TTL).
In other words, there are solutions that you can use aside from pure view state or pure query methods.
answered Nov 12 at 16:07
sfdcfox
239k10182401
239k10182401
2
+1 from me, love the JSON serialize idea!
– codeyinthecloud
Nov 12 at 16:16
add a comment |
2
+1 from me, love the JSON serialize idea!
– codeyinthecloud
Nov 12 at 16:16
2
2
+1 from me, love the JSON serialize idea!
– codeyinthecloud
Nov 12 at 16:16
+1 from me, love the JSON serialize idea!
– codeyinthecloud
Nov 12 at 16:16
add a comment |
up vote
1
down vote
When Can I Use Platform Cache?
You can use Platform Cache in your code almost anywhere you access the same data over and over. Using cached data improves the performance of your app and is faster than performing SOQL queries repetitively, making multiple API calls, or computing complex calculations.
The best data to cache is:
- Reused throughout a session, or reused across all users and requests
- Static (not rapidly changing)
- Expensive to compute or retrieve
Store Data That Doesn’t Change Often
Use the cache to store static data or data that doesn’t change often. This data is initially retrieved through API calls from a third party or locally through SOQL queries. If the data changes, cache this data if the values don’t have to be highly accurate at all times.
Store Data Obtained from Complex Calculations
Values that are a result of complex calculations or long queries are good candidates for cache storage. Examples of such data are:
- Total sales over the past week
- Total volunteering hours company employees did as a whole
- Top sales ranking
For clues on where to use Platform Cache, inspect your code. For example, do you currently store app data by overloading a Visualforce view state? These stored values are all candidates for Platform Cache.
Not every use case is a Platform Cache use case. For example, data that changes often and that is real-time, such as stock quotes, isn’t a good candidate for caching. Also, ensure you familiarize yourself with Platform Cache limitations. For example, if your data is accessed by asynchronous Apex, it can’t be stored in a cache that is based on the user’s session.
Keep in mind these considerations when working with Platform Cache.
- Cache isn’t persisted. There’s no guarantee against data loss.
- Some or all cache is invalidated when you modify an Apex class in your org.
- Data in the cache isn’t encrypted.
- Org cache supports concurrent reads and writes across multiple simultaneous Apex transactions. For example, a transaction updates the key PetName with the value Fido. At the same time, another transaction updates the same key with the value Felix. Both writes succeed, but one of the two values is chosen arbitrarily as the winner, and later transactions read that one value. However, this arbitrary choice is per key rather than per transaction. For example, suppose one transaction writes PetType="Cat" and PetName="Felix". Then, at the same moment, another transaction writes PetType="Dog" and PetName="Fido". In this case, the PetType winning value could be from the first transaction, and the PetName winning value could be from the second transaction. Subsequent get() calls on those keys would return PetType="Cat" and PetName="Fido".
- Cache misses can happen. We recommend constructing your code to consider a case where previously cached items aren’t found. Alternatively, use the CacheBuilder Interface, which checks for cache misses.
- Session cache doesn’t support asynchronous Apex. For example, you can’t use future methods or batch Apex with session cache.
- Partitions must adhere to the limits within Salesforce.
- The session cache can store values up to eight hours. The org cache can store values up to 48 hours.
add a comment |
up vote
1
down vote
When Can I Use Platform Cache?
You can use Platform Cache in your code almost anywhere you access the same data over and over. Using cached data improves the performance of your app and is faster than performing SOQL queries repetitively, making multiple API calls, or computing complex calculations.
The best data to cache is:
- Reused throughout a session, or reused across all users and requests
- Static (not rapidly changing)
- Expensive to compute or retrieve
Store Data That Doesn’t Change Often
Use the cache to store static data or data that doesn’t change often. This data is initially retrieved through API calls from a third party or locally through SOQL queries. If the data changes, cache this data if the values don’t have to be highly accurate at all times.
Store Data Obtained from Complex Calculations
Values that are a result of complex calculations or long queries are good candidates for cache storage. Examples of such data are:
- Total sales over the past week
- Total volunteering hours company employees did as a whole
- Top sales ranking
For clues on where to use Platform Cache, inspect your code. For example, do you currently store app data by overloading a Visualforce view state? These stored values are all candidates for Platform Cache.
Not every use case is a Platform Cache use case. For example, data that changes often and that is real-time, such as stock quotes, isn’t a good candidate for caching. Also, ensure you familiarize yourself with Platform Cache limitations. For example, if your data is accessed by asynchronous Apex, it can’t be stored in a cache that is based on the user’s session.
Keep in mind these considerations when working with Platform Cache.
- Cache isn’t persisted. There’s no guarantee against data loss.
- Some or all cache is invalidated when you modify an Apex class in your org.
- Data in the cache isn’t encrypted.
- Org cache supports concurrent reads and writes across multiple simultaneous Apex transactions. For example, a transaction updates the key PetName with the value Fido. At the same time, another transaction updates the same key with the value Felix. Both writes succeed, but one of the two values is chosen arbitrarily as the winner, and later transactions read that one value. However, this arbitrary choice is per key rather than per transaction. For example, suppose one transaction writes PetType="Cat" and PetName="Felix". Then, at the same moment, another transaction writes PetType="Dog" and PetName="Fido". In this case, the PetType winning value could be from the first transaction, and the PetName winning value could be from the second transaction. Subsequent get() calls on those keys would return PetType="Cat" and PetName="Fido".
- Cache misses can happen. We recommend constructing your code to consider a case where previously cached items aren’t found. Alternatively, use the CacheBuilder Interface, which checks for cache misses.
- Session cache doesn’t support asynchronous Apex. For example, you can’t use future methods or batch Apex with session cache.
- Partitions must adhere to the limits within Salesforce.
- The session cache can store values up to eight hours. The org cache can store values up to 48 hours.
add a comment |
up vote
1
down vote
up vote
1
down vote
When Can I Use Platform Cache?
You can use Platform Cache in your code almost anywhere you access the same data over and over. Using cached data improves the performance of your app and is faster than performing SOQL queries repetitively, making multiple API calls, or computing complex calculations.
The best data to cache is:
- Reused throughout a session, or reused across all users and requests
- Static (not rapidly changing)
- Expensive to compute or retrieve
Store Data That Doesn’t Change Often
Use the cache to store static data or data that doesn’t change often. This data is initially retrieved through API calls from a third party or locally through SOQL queries. If the data changes, cache this data if the values don’t have to be highly accurate at all times.
Store Data Obtained from Complex Calculations
Values that are a result of complex calculations or long queries are good candidates for cache storage. Examples of such data are:
- Total sales over the past week
- Total volunteering hours company employees did as a whole
- Top sales ranking
For clues on where to use Platform Cache, inspect your code. For example, do you currently store app data by overloading a Visualforce view state? These stored values are all candidates for Platform Cache.
Not every use case is a Platform Cache use case. For example, data that changes often and that is real-time, such as stock quotes, isn’t a good candidate for caching. Also, ensure you familiarize yourself with Platform Cache limitations. For example, if your data is accessed by asynchronous Apex, it can’t be stored in a cache that is based on the user’s session.
Keep in mind these considerations when working with Platform Cache.
- Cache isn’t persisted. There’s no guarantee against data loss.
- Some or all cache is invalidated when you modify an Apex class in your org.
- Data in the cache isn’t encrypted.
- Org cache supports concurrent reads and writes across multiple simultaneous Apex transactions. For example, a transaction updates the key PetName with the value Fido. At the same time, another transaction updates the same key with the value Felix. Both writes succeed, but one of the two values is chosen arbitrarily as the winner, and later transactions read that one value. However, this arbitrary choice is per key rather than per transaction. For example, suppose one transaction writes PetType="Cat" and PetName="Felix". Then, at the same moment, another transaction writes PetType="Dog" and PetName="Fido". In this case, the PetType winning value could be from the first transaction, and the PetName winning value could be from the second transaction. Subsequent get() calls on those keys would return PetType="Cat" and PetName="Fido".
- Cache misses can happen. We recommend constructing your code to consider a case where previously cached items aren’t found. Alternatively, use the CacheBuilder Interface, which checks for cache misses.
- Session cache doesn’t support asynchronous Apex. For example, you can’t use future methods or batch Apex with session cache.
- Partitions must adhere to the limits within Salesforce.
- The session cache can store values up to eight hours. The org cache can store values up to 48 hours.
When Can I Use Platform Cache?
You can use Platform Cache in your code almost anywhere you access the same data over and over. Using cached data improves the performance of your app and is faster than performing SOQL queries repetitively, making multiple API calls, or computing complex calculations.
The best data to cache is:
- Reused throughout a session, or reused across all users and requests
- Static (not rapidly changing)
- Expensive to compute or retrieve
Store Data That Doesn’t Change Often
Use the cache to store static data or data that doesn’t change often. This data is initially retrieved through API calls from a third party or locally through SOQL queries. If the data changes, cache this data if the values don’t have to be highly accurate at all times.
Store Data Obtained from Complex Calculations
Values that are a result of complex calculations or long queries are good candidates for cache storage. Examples of such data are:
- Total sales over the past week
- Total volunteering hours company employees did as a whole
- Top sales ranking
For clues on where to use Platform Cache, inspect your code. For example, do you currently store app data by overloading a Visualforce view state? These stored values are all candidates for Platform Cache.
Not every use case is a Platform Cache use case. For example, data that changes often and that is real-time, such as stock quotes, isn’t a good candidate for caching. Also, ensure you familiarize yourself with Platform Cache limitations. For example, if your data is accessed by asynchronous Apex, it can’t be stored in a cache that is based on the user’s session.
Keep in mind these considerations when working with Platform Cache.
- Cache isn’t persisted. There’s no guarantee against data loss.
- Some or all cache is invalidated when you modify an Apex class in your org.
- Data in the cache isn’t encrypted.
- Org cache supports concurrent reads and writes across multiple simultaneous Apex transactions. For example, a transaction updates the key PetName with the value Fido. At the same time, another transaction updates the same key with the value Felix. Both writes succeed, but one of the two values is chosen arbitrarily as the winner, and later transactions read that one value. However, this arbitrary choice is per key rather than per transaction. For example, suppose one transaction writes PetType="Cat" and PetName="Felix". Then, at the same moment, another transaction writes PetType="Dog" and PetName="Fido". In this case, the PetType winning value could be from the first transaction, and the PetName winning value could be from the second transaction. Subsequent get() calls on those keys would return PetType="Cat" and PetName="Fido".
- Cache misses can happen. We recommend constructing your code to consider a case where previously cached items aren’t found. Alternatively, use the CacheBuilder Interface, which checks for cache misses.
- Session cache doesn’t support asynchronous Apex. For example, you can’t use future methods or batch Apex with session cache.
- Partitions must adhere to the limits within Salesforce.
- The session cache can store values up to eight hours. The org cache can store values up to 48 hours.
edited Nov 12 at 16:23
answered Nov 12 at 16:05
codeyinthecloud
2,202319
2,202319
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsalesforce.stackexchange.com%2fquestions%2f239067%2fmaintain-state-in-visualforce-page-without-using-view-state%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown