+ Start a Discussion
MJ09MJ09 

"Error occurred when processing your submitted information" for protected custom setting

I have a managed package that includes a Protected Hierarchy-type Custom Setting named Config__c that contains configuration values. The following code fetches the Config__c record for the current user:

    public static Config__c QueryConfigData() {
Config__c config = Config__c.getValues(UserInfo.getUserId());
if (config == null) {
// The current user has no value for this custom setting - create one
config = new Config__c();
config.setupownerid = UserInfo.getUserId();
config.Show__c = '-All-';
insert config;
}

return config;
}

 

The following code saves a given Config__c record that was fetched using the method above:

    public static void SaveConfigData(Config__c config) {
update config;
}

 

Both methods are called from a custom controller. I don't know whether it's relevant, but the custom controller is defined "without sharing." The controller calls the query method in an apex:page action method, and when the user clicks a Save button, it calls the save method.

I've tested this in the development org, running as both a System Admin and as a Standard User, and it works great.

However, when I install the managed package in another org, while it works when I'm running as a System Admin, when I'm running as a Standard User, the save method generates the error "An error occurred when processing your submitted information."

Because the custom setting is defined as Protected in a managed package, I can't see whether custom setting data is being created for the non-System Admin user. The error doesn't happen on the insert in the query method, but there seems to be a problem updating the custom setting in the save method.

Any ideas?

 

MJ09MJ09

Finally found the problem. It wasn't in the Apex code at all. The problem was that my VF page was binding the custom setting's fields to inputField, inputCheckbox, and other form components. Apparently, doing that in a managed package with a protected custom setting caused problems when an action method was called and the form components were sent back to the controller.

 

(It's possible that either the managed package or the protected nature of the custom setting doesn't factor into the problem. That's just the environment where I ran into it.)

 

The work-around is to not bind your VF page's form components to custom setting fields. Instead, create variables in your controller (e.g., a String variable), bind your form components to those variables, and have code in your controller that copies values back and forth between the custom setting's fields and the controller's variables. A pain, but at least it works.