• Pulkit Sood 6
  • NEWBIE
  • 0 Points
  • Member since 2017

  • Chatter
    Feed
  • 0
    Best Answers
  • 1
    Likes Received
  • 0
    Likes Given
  • 2
    Questions
  • 2
    Replies

hi, I am new to salesforce, was given a dummy project as training purpose after reading about view state and execution order, context,etc. i am a bit confused about how to save the state(as with static vairables) through multiple request to the Server, i can be mistaking but that's what i got till now,

static vars can't be used because, HTTP, being stateless, removes them after every request in the VFpage

can use  view state but that would increase transmission lag, can use transient variables(to have one non-transient "id" variable and retreieve rest of the record fields), but that would require querying everytime a user makes database request,

can anyone tell me when to consider using the transient variable method and when view state?? Thanx

Static variable is not retaining its value

public class trial {
        Integer x=0;
        static Integer y=0;
    public void setX(Integer i){
        x=i;
    }
    
    public Integer getX(){
        return x;
    }
    
    public pageReference ChangeValue(){
        //action
        System.debug('action x - '+x+' to '+(++x)+' and y - '+y);
        return null;
    }
    
    public void getChangeValue(){
        //oclick
        if(y==0){System.debug(this+' onclick y - '+y+' to '+(++y)+' & x - '+x+' to '+(++x));return;}
        System.debug(this+' onclick x - '+x+' to '+(++x)+' and y - '+y);
        
    }
   
}
------------------------------------------------------------------------------------------------------------------------------------------------------

When page loads, onClick is run automatically, why?? but anyways it should set the value for static y=1, i.e. next time after "if" statements should run
it debugs as "y=0 to y=1"
then when i click the button it again debugs as "y=0 to y=1" i.e. the class isn't retaining Static variable's value, but would work/retain-its-value if i remove STATIC KEYWORD from defination. i.e. make it an instance variable instance
but, with static, it debugs as "y=0 to y=1" every time i click the button, though it should have been set as right after the first debug
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
VF:-
<apex:page controller="trial">
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"/>
    <apex:form >
              <apex:commandButton value="Change and Set Value" onClick="{!changeValue}" action="{!changeValue}"/>
    </apex:form>
    <script>
    var x;
    function setValue(){
    var op=$("label[id$='oplbl']"); 
        x ='{!X}';
        alert('x - '+x);
        op.text(x+'');
        return x;
    }
    </script>
</apex:page>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

X IS INSTANCE VAR AND  Y IS STATIC Var
AFTER PAGE LOADS, ON CLICK METHOD RUNS BY ITSELF:-
13:08:52:009 USER_DEBUG [20]|DEBUG|trial:[x=0, y=0] onclick y - 0 to 1 & x - 0 to 1

WHEN I CLICK:-
13:10:13:009 USER_DEBUG [14]|DEBUG|action x - 1 to 2 and y - 0
13:10:13:013 USER_DEBUG [20]|DEBUG|trial:[x=2, y=0] onclick y - 0 to 1 & x - 2 to 3   //second execution for onclick method must have the y's value=1, but it rather preserves x's value

WHEN I CLICK AGAIN:-
13:14:17:009 USER_DEBUG [14]|DEBUG|action x - 3 to 4 and y - 0
13:14:17:013 USER_DEBUG [20]|DEBUG|trial:[x=4, y=0] onclick y - 0 to 1 & x - 4 to 5
Static variable is not retaining its value

public class trial {
        Integer x=0;
        static Integer y=0;
    public void setX(Integer i){
        x=i;
    }
    
    public Integer getX(){
        return x;
    }
    
    public pageReference ChangeValue(){
        //action
        System.debug('action x - '+x+' to '+(++x)+' and y - '+y);
        return null;
    }
    
    public void getChangeValue(){
        //oclick
        if(y==0){System.debug(this+' onclick y - '+y+' to '+(++y)+' & x - '+x+' to '+(++x));return;}
        System.debug(this+' onclick x - '+x+' to '+(++x)+' and y - '+y);
        
    }
   
}
------------------------------------------------------------------------------------------------------------------------------------------------------

When page loads, onClick is run automatically, why?? but anyways it should set the value for static y=1, i.e. next time after "if" statements should run
it debugs as "y=0 to y=1"
then when i click the button it again debugs as "y=0 to y=1" i.e. the class isn't retaining Static variable's value, but would work/retain-its-value if i remove STATIC KEYWORD from defination. i.e. make it an instance variable instance
but, with static, it debugs as "y=0 to y=1" every time i click the button, though it should have been set as right after the first debug
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
VF:-
<apex:page controller="trial">
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"/>
    <apex:form >
              <apex:commandButton value="Change and Set Value" onClick="{!changeValue}" action="{!changeValue}"/>
    </apex:form>
    <script>
    var x;
    function setValue(){
    var op=$("label[id$='oplbl']"); 
        x ='{!X}';
        alert('x - '+x);
        op.text(x+'');
        return x;
    }
    </script>
</apex:page>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

X IS INSTANCE VAR AND  Y IS STATIC Var
AFTER PAGE LOADS, ON CLICK METHOD RUNS BY ITSELF:-
13:08:52:009 USER_DEBUG [20]|DEBUG|trial:[x=0, y=0] onclick y - 0 to 1 & x - 0 to 1

WHEN I CLICK:-
13:10:13:009 USER_DEBUG [14]|DEBUG|action x - 1 to 2 and y - 0
13:10:13:013 USER_DEBUG [20]|DEBUG|trial:[x=2, y=0] onclick y - 0 to 1 & x - 2 to 3   //second execution for onclick method must have the y's value=1, but it rather preserves x's value

WHEN I CLICK AGAIN:-
13:14:17:009 USER_DEBUG [14]|DEBUG|action x - 3 to 4 and y - 0
13:14:17:013 USER_DEBUG [20]|DEBUG|trial:[x=4, y=0] onclick y - 0 to 1 & x - 4 to 5
Static variable is not retaining its value

public class trial {
        Integer x=0;
        static Integer y=0;
    public void setX(Integer i){
        x=i;
    }
    
    public Integer getX(){
        return x;
    }
    
    public pageReference ChangeValue(){
        //action
        System.debug('action x - '+x+' to '+(++x)+' and y - '+y);
        return null;
    }
    
    public void getChangeValue(){
        //oclick
        if(y==0){System.debug(this+' onclick y - '+y+' to '+(++y)+' & x - '+x+' to '+(++x));return;}
        System.debug(this+' onclick x - '+x+' to '+(++x)+' and y - '+y);
        
    }
   
}
------------------------------------------------------------------------------------------------------------------------------------------------------

When page loads, onClick is run automatically, why?? but anyways it should set the value for static y=1, i.e. next time after "if" statements should run
it debugs as "y=0 to y=1"
then when i click the button it again debugs as "y=0 to y=1" i.e. the class isn't retaining Static variable's value, but would work/retain-its-value if i remove STATIC KEYWORD from defination. i.e. make it an instance variable instance
but, with static, it debugs as "y=0 to y=1" every time i click the button, though it should have been set as right after the first debug
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
VF:-
<apex:page controller="trial">
    <apex:includeScript value="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"/>
    <apex:form >
              <apex:commandButton value="Change and Set Value" onClick="{!changeValue}" action="{!changeValue}"/>
    </apex:form>
    <script>
    var x;
    function setValue(){
    var op=$("label[id$='oplbl']"); 
        x ='{!X}';
        alert('x - '+x);
        op.text(x+'');
        return x;
    }
    </script>
</apex:page>
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

X IS INSTANCE VAR AND  Y IS STATIC Var
AFTER PAGE LOADS, ON CLICK METHOD RUNS BY ITSELF:-
13:08:52:009 USER_DEBUG [20]|DEBUG|trial:[x=0, y=0] onclick y - 0 to 1 & x - 0 to 1

WHEN I CLICK:-
13:10:13:009 USER_DEBUG [14]|DEBUG|action x - 1 to 2 and y - 0
13:10:13:013 USER_DEBUG [20]|DEBUG|trial:[x=2, y=0] onclick y - 0 to 1 & x - 2 to 3   //second execution for onclick method must have the y's value=1, but it rather preserves x's value

WHEN I CLICK AGAIN:-
13:14:17:009 USER_DEBUG [14]|DEBUG|action x - 3 to 4 and y - 0
13:14:17:013 USER_DEBUG [20]|DEBUG|trial:[x=4, y=0] onclick y - 0 to 1 & x - 4 to 5