function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
jadentjadent 

Custom Settings to hold User Preferences Permission Issue

i was hoping to use a hierarchical custom settings object to store user preferences for an app. This would require the user to create and edit their custom setting record.

 

From what i have read this would require every user to have the "Customize Application" permission to create and edit their own custom setting record. Obviously we cannot do that.

 

Is there some way to get around this with a vfpage & apex? So the user can goto a vfpage and create/edit the custom settings object record so they can manage their preferences?

Best Answer chosen by Admin (Salesforce Developers) 
mtbclimbermtbclimber

So I just did a basic test and it appears there may be an issue with binding directly to the setting object.  If you bind your form inputs against properties or an apex object that addressed the issue for me, i.e. this threw an error on save for a non-admin user:

 

 

<apex:page controller="EditMySettingCon">
<apex:form >
<apex:pageMessages />
<apex:inputCheckbox value="{!setting.Enabled__c}"/>
<apex:commandButton value="Save" action="{!save}"/>
</apex:form>
</apex:page>

 

public class EditMySettingCon {

public EditMySettingCon() {
setting = My_Setting__c.getValues(UserInfo.getUserId());
if (setting == null) { setting = new My_Setting__c(setupownerid = UserInfo.getUserId()); }
}

public My_Setting__c setting { get; private set; }

public void save() {
try {
Database.upsert(setting);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Setting upserted!'));
} catch (system.exception e) {
ApexPages.addMessages(e);
}
}
}

 

 

but this did not:

 

 

<apex:page controller="EditMySettingCon">
    <apex:form >
        <apex:pageMessages />
        <apex:inputCheckbox value="{!enabled}"/>
        <apex:commandButton value="Save" action="{!save}"/>
    </apex:form>
</apex:page>

 

public class EditMySettingCon {

    public EditMySettingCon() {
        setting = My_Setting__c.getValues(UserInfo.getUserId());
        if (setting == null) { setting = new My_Setting__c(setupownerid = UserInfo.getUserId()); }
        enabled = setting.enabled__c;
    }

    private My_Setting__c setting { get; set; }
    public boolean enabled        { get; set; }
    
    public void save() {
        try {
            setting.enabled__c = enabled;
            Database.upsert(setting);
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Setting upserted!'));
        } catch (system.exception e) {
            ApexPages.addMessages(e);
        }
    }
}

 

 

I'm checking with the team to see if this is a bug. I'll update this thread with the answer.

 

Let me know if that workaround addresses your issue.

 

Regards,

Andrew

 

 

 

 

All Answers

mtbclimbermtbclimber

Yes, you can expose a settings management page built in Visualforce, backed by an Apex controller which will allow the user to manage their settings.  This works because Apex runs in system mode.  You control access to execute Apex at the entry points - class-level security for webservice methods and page-level security for Visualforce page invocations. Within Apex you can detect the running user's permissions and choose how you would like to behave accordingly.

 

Hope that helps. Post back here if you need more assistance.

 

Regards,

jadentjadent

thanks mt. we've done that and it still will not create or edit the custom settings record unless with give the Customize Application permission on their profile. Is there something we need to do specifically to the controller?

mtbclimbermtbclimber

There shouldn't be anything to do beyond what it seems you've done. What user type can't update the setting info through Apex and what error are you getting?

mtbclimbermtbclimber

So I just did a basic test and it appears there may be an issue with binding directly to the setting object.  If you bind your form inputs against properties or an apex object that addressed the issue for me, i.e. this threw an error on save for a non-admin user:

 

 

<apex:page controller="EditMySettingCon">
<apex:form >
<apex:pageMessages />
<apex:inputCheckbox value="{!setting.Enabled__c}"/>
<apex:commandButton value="Save" action="{!save}"/>
</apex:form>
</apex:page>

 

public class EditMySettingCon {

public EditMySettingCon() {
setting = My_Setting__c.getValues(UserInfo.getUserId());
if (setting == null) { setting = new My_Setting__c(setupownerid = UserInfo.getUserId()); }
}

public My_Setting__c setting { get; private set; }

public void save() {
try {
Database.upsert(setting);
ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Setting upserted!'));
} catch (system.exception e) {
ApexPages.addMessages(e);
}
}
}

 

 

but this did not:

 

 

<apex:page controller="EditMySettingCon">
    <apex:form >
        <apex:pageMessages />
        <apex:inputCheckbox value="{!enabled}"/>
        <apex:commandButton value="Save" action="{!save}"/>
    </apex:form>
</apex:page>

 

public class EditMySettingCon {

    public EditMySettingCon() {
        setting = My_Setting__c.getValues(UserInfo.getUserId());
        if (setting == null) { setting = new My_Setting__c(setupownerid = UserInfo.getUserId()); }
        enabled = setting.enabled__c;
    }

    private My_Setting__c setting { get; set; }
    public boolean enabled        { get; set; }
    
    public void save() {
        try {
            setting.enabled__c = enabled;
            Database.upsert(setting);
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.CONFIRM, 'Setting upserted!'));
        } catch (system.exception e) {
            ApexPages.addMessages(e);
        }
    }
}

 

 

I'm checking with the team to see if this is a bug. I'll update this thread with the answer.

 

Let me know if that workaround addresses your issue.

 

Regards,

Andrew

 

 

 

 

This was selected as the best answer
jadentjadent

thats funny we just figured that out ourselves and came back to post about it.

Crazy little bug took me forever to figure it out. The weird thing was it never even threw any error, even in a try catch. It would through show the debu log as /apex/apex/test2 instead of /apex/test2 but with no errors. very odd.

 

thanks for the help!

mtbclimbermtbclimber

Nice :)

 

Still discussing what the bug is here. Clearly the current behavior is not correct.

 

More to come.

 

Thanks for your patience and glad you are moving forward again.

 

Regards,

Marc C.Marc C.

Thanks for this code!

 

Do you know how to get the label for a custom setting field in apex?

 

Tried variations on CustomSetting__c.Field1__c.Label to no avail.

michael_mcmahonmichael_mcmahon
Hi Andrew,

I know this is an old thread, but these are where I learn Apex and VF. Was there ever a resolution to this? Thanks