I'm wondering if it's possible to call a PageReference method in a controller without the user having to click on any vf page buttons. For example, I have the following condition…(see below)…and I want to call the customSave() method based on a boolean statement, but it's not working.
public String getJSONString(){
String fault = 'fault';
if(this.jsonString.containsNone(fault))
{
customSave();
}
return.this.jsonString;
}
public PageReference customSave(){
//do some stuff
stdCtrl.save();
PageReference redirectPage = new PageReference('apex/newpage');
redirectPage.setRedirect(true);
return redirectPage;
}
Best Answer
Certainly. Functions can generally be called from any other function. However, I'm under the impression that you have your jsonString variable defined as:
Since you're also using
getJsonString()
as a function, this will shadow (read: override) the public getter and setter on the variable itself; the controller won't be able to see the value you're assigning, as it will effectively be read-only. I'm not sure why you're not getting a compilation error, but you should be able to fix your problem by adding the following code:Please note, however, that DML operations during the getter-setter phase are dangerous (and usually not allowed), which is probably why you're not seeing the expected results. Instead, consider calling an action, and then performing your save during the action method:
Note that it isn't necessary to check the return value, but any DML exceptions in your code should be handled appropriately (e.g. if stdCtrl.save() returns a null page reference, display an error).