Sometimes you have to load user preferences stored in a Hierarchy custom setting when a Visualforce page loads. If the preferences exist, then retrieve them, otherwise create them and insert them.
Something like
if(My_Preferences__c.getValues(UserInfo.getUserId()) != null){
prefs = My_Preferences__c.getValues(UserInfo.getUserId());
}else{
prefs = new My_Preferences__c(SetupOwnerId = UserInfo.getUserId());
insert prefs;
}
The only problem is, you cannot perform DML in the constructor. Whenever this issue comes up on discussion boards, the answer is always the same, use the action="your preferences method" parameter in the apex:page section. I even remember a guy who worked at Salesforce saying to do this.
And yet, whenever you put an action in the apex:page, the Security Scanner always flags it as a Critical item, and surprise surprise, clients aren't too hot about critical items.
I do remember trying to call the method via an apex:actionfunction in a $(document).ready(function() { });) call but this caused the page components to rerender unnecessarily and caused "flashing". Besides, I only need to insert the user preferences the first time they click on the page.
Has anyone encountered this problem before, and if so, how did they solve it?