function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Matthew BathersbyMatthew Bathersby 

Visualforce Data Loss on ReRender

I have a simple controller that is designed to act as a time. As so:

public class timerController {

    public static dateTime startTime {get;set;}
    public static dateTime stopTime {get;set;}
 
    public static void startTimer(){

        startTime = system.now();

        system.debug(startTime);
    }
    
    public static void stopTimer(){
        
        stopTime = system.now();

        system.debug(startTime);
        system.debug(stopTime);
    }

}

Each method is tied to a button ('Start Timer' and 'Stop Timer'). When I run the startTimer method, I can see the startTime value in my debug.
When I run the stopTimer method, I can see the stopTime value in my debug, but the startTime has been reset to null.

Why is the startTime value not being maintained in the state? I know I must be missing something small, but I can't for the life of me spot it.

Any suggestions would be greatly appreciated.

SeAlVaSeAlVa
Hi,
you just have to remove 'static' keyword from your code.
Regards

If this post helped you, please mark it as "Best Answer"; otherwise reply with your doubts.
George thomasGeorge thomas
Hi,

Http protocola are stateless and salesforce introduced view state to maintain the http elemts data. But there are 2 exception in that case, the variables which are static and transient doesn't maintain the state on the page. Please dont use static ,transient if you want your page to retain the variable values. Thanks!!
Matthew BathersbyMatthew Bathersby
Thanks folks. I knew it would be something simple. This did the trick nicely!