one of our business requirements is that the data in a certain field in a form is not persisted as the user navigates different objects in salesforce.

Without getting into too much detail, we have been able to implement this business requirement by adding the transient keyword to the field.

The field now looks like this:

public transient String Note { get; set; }

Given that this has now become a requirement, we want to automate the testing of the acceptance criteria to avoid a regression in the future (for instance, a developer removing the transient keyword, etc).

Are there any ways to automatically test this? Could I use Reflection or describe to read that field and know whether it's transient? Is there some kind of Unit Test pattern that would allow me to test that a field is essentially transient and not transmitted in the viewstate?


1 Answer 1


A transient field won't be serialized. Either to viewstate or to JSON. Viewstate is a bit difficult in a test context, but JSON isn't.

Use JSON.serialise(object) to create a string representation and then assert that is doesn't contain your transient property.


public class SomeController {
    public transient String Note { get; set; }
    public string Foo { get; set; }

SomeController sc = new SomeController();
sc.Foo = 'Bar';
sc.Note = 'Shhh!';
string jsonSc = JSON.serialize(sc);
  • 1
    very crafty Daniel! Aug 22, 2014 at 9:24
  • Daniel -- this is pretty cool; have you got any rules of thumb/algos to infer from the JSON size anything about the viewstate size? I would guess not as viewstate includes VF components unknown to the apex side. The above verifies that transient isn't inadvertently removed but doesn't test to see if new non-transient properties are added that could blow viewstate. We really need a PageReference method getViewStateSize()
    – cropredy
    Apr 30, 2016 at 1:56
  • @cropredy I haven't tested to see what the relationship between JSON size and viewstate size is. It would be an interesting experiment to run. If the serialization used by the viewstate differs significantly from JSON there might not be much of a correlation. The component tree and internal housekeeping parts of the viewstate would make it hard to estimate the size by object structure alone. May 1, 2016 at 7:47
  • Where in code could you use a getViewStateSize() method? The view state isn't created until step 4 in the order of execution if an apex:form is present. By that state all the controller code would have executed. May 1, 2016 at 7:50
  • Good point, perhaps the method would work after Test.stopTest(); I'm sort of astral-projecting here. I adapted your answer here and used it in a testmethod for regression testing viewstate growth. See cropredysfdc.com/2016/04/30/… - be curious what you think.
    – cropredy
    May 1, 2016 at 20:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.