I just got a strange recommendation from SalesForce support which was related to inability of APEX controller class to save value from VisualForce component to a custom setting.
The recommendation was literally "DML inside the property's getter and setter are not a good practice."
Here is what does not work:
...
public String currentFunction {
get {
if (null == currentFunction) {
return getCurrentFunction();
}
return currentFunction;
}
set {
currentFunction = value;
// these lines:
TestSetting__c settings = TestSetting__c.getInstance();
settings.Function__c = value;
upsert settings;
}
}
...
The controller code above does not produce any exceptions, logs show the setting has been saved, but in reality it does not. I found nothing explaining that anywhere, including Apex Properties page on the dev guide.
I spent a month or so to solve this issue with the support, so i decided to share (partial!) solution for this case to give you a chance to not waste your time.
Update #1: as per Controller Methods dev guide,
It’s a best practice for setter methods to be idempotent, that is, to not have side effects. For example, don’t increment a variable, write a log message, or add a new record to the database. Visualforce doesn’t define the order in which setter methods are called, or how many times they might be called in the course of processing a request. Design your setter methods to produce the same outcome, whether they are called once or multiple times for a single page request.
IMO, that does not directly explain my case, but gives some useful recommendations. but my biggest concern is — why does that happen silently?