5

I'm trying to create a VisualForce configuration page for my managed package. I have a hierarchal custom setting field for an authentication key which is needed to access a remote service. I can read this value with "{!$Setup.prefix__Settings__c.prefix__Key__c}"

however, if I try making an input field for it using

<apex:inputField value="{!$Setup.prefix__Settings__c.prefix__Key__c}"/>

it says that only sObjects work for inputField values.

I thought merge fields worked too...anyway, how can I let the user type in a key and save that value to the custom settings record? Also, would using a List custom setting be easier or better?

6

You can set them via Apex controller. Here is some documentation

So it would be something like this (for hierarchy settings):

Apex:

public String myValueFromPage{get; set;}

public void saveMyValue() {
    prefix__MySettings__c settings = prefix__MySettings__c.getInstance();
    settings.prefix__myField__c = myValueFromPage;
    upsert settings;
}

Visualforce:

<apex:inputText value="{!myValueFromPage}">
<apex:commandButton action="{!saveMyValue}" value="Save Input"/>
  • Can you add more information about SetupOwnerId to your answer? If a User or Profile level setting exists, the change your code makes will have no effect because you are modifying the "Default Organization Level" value. – Keith C Jun 4 '15 at 16:20
  • @KeithC you're right, thanks for noticing. I've updated the setting instantiation to make it less confusing :) – bananka Jun 4 '15 at 16:26
0

Sample code from this site.User should have Customize Application permissions then below code works

<apex:page controller="SuperDuperSettingController">
    <apex:pageMessages />
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockSection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Name</apex:outputLabel>
                    <apex:inputText value="{!setting.Name}"/>                
                </apex:pageBlockSectionItem>
            </apex:pageBlockSection>
            <apex:pageBlockButtons >
                <apex:commandButton action="{!save}" value="Save" />
            </apex:pageBlockButtons>
        </apex:pageBlock>
    </apex:form>
</apex:page>

    public class SuperDuperSettingController {

    public SettingProxy setting {get; set;}

    public SuperDuperSettingController() {
        String settingId = ApexPages.currentPage().getParameters().get( 'id' );
        if (settingId != null ) {
            setting = new SettingProxy([SELECT Id, Name FROM SuperDuperSetting__c WHERE Id = :settingId]);
        } else {
            setting = new SettingProxy();
        }
    }

    public PageReference save() {
        if (setting.Id != null) {
            update setting.getSuperDuperSetting();
        } else {
            setting.insertSetting();
        }
        PageReference pr = Page.SuperDuperSetting;
        pr.getParameters().put('Id', setting.Id);
        pr.setRedirect(true);
        return pr;
    }

    public class SettingProxy {
        public Id Id {get;set;}
        public String Name {get;set;}

        public SettingProxy(SuperDuperSetting__c sdp) {
            this.Id = sdp.Id;
            this.Name = sdp.Name;
        }

        public SettingProxy() {}

        public SuperDuperSetting__c getSuperDuperSetting() {
            return new SuperDuperSetting__c(
                Id = this.Id,
                Name = this.Name
            );
        }

        public void insertSetting() {
            SuperDuperSetting__c sdp = this.getSuperDuperSetting();
            insert sdp;
            this.Id = sdp.Id;
        }

    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.