0

Starting date of current week is not displaying on VF page (blank data), but coming in debug logs.

Controller Code:

public with sharing class Dummy {

public date weekStart {get;set;}

public void MyAction () {      
    date myDate = system.today();
    date weekStart = myDate.toStartofWeek();
    system.debug('Week Start Date' + weekStart);    
}

}

Visualforce page:

    Week Start Date : {!weekStart}

1

Please remove the new declaration on the weekStart variable Because of that declaration, the weekStart you fill is not the same member variable you have declared on the first line of your class, it's the private variable you created on the MyAction method.

The suggested fix (for the method):

public void MyAction () {      
    date myDate = system.today();
    weekStart = myDate.toStartofWeek();//removed the variable declartion
    system.debug('Week Start Date' + weekStart);    
}
0

depending on your use case, you may be able to simplify this to:

// default weekstart upon page presentation
public date weekstart {
  get { return weekstart == null ? weekstart = System.today.toStartofWeek() : weekstart;}
  set;
}

public void myAction() {
  // presumes myAction needs to reset weekstart to initial value
  this.weekstart = null;
}

Any reference to weekstart either within the controller or the VF page will access the weekstart's getter whichm if the value is null, will set and return value to current week's start date

If you are trying to default the weekstart date in the page action, and that is the only thing you are doing in that page action, you can avoid the page action altogether and just use the getter in my answer.

0

Here is your solution:

public date weekStart {get;set;}

public void MyAction () {      
    date myDate = system.today();
    this.weekStart = myDate.toStartofWeek(); 
}

You should use this. to access member class variable. If you don't, you could be accessing your function temporal variable.

Take in mind:

public class Example {
    Private integer myVar;

    public void myFunction(int myVar) {
        // Store function parameter 'myVar' in class variable 'myVar'.
        this.myVar = myVar;

        // Modify function parameter 'myVar'
        myVar = 7;

        // Modify class variable 'myVar'
        this.myVar = 8;
    }
}

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.