0

Req: I have a command button on VF page.

When i click on it then a popup window needs to open (That can be achievable). When we add to some values those need to get saved into custom settings and those need to be available in a multi-select picklist on same page.

  1. Need to save into custom settings from visualforce page.
  2. Those values needs to get refresh from custom settings to that mulitselect picklist field on same VF page.

Any Suggestions or any sample codes regarding above two points please provide me.

/***************VF page******************************/
<apex:page controller="Sample_Groups" sidebar="false" >
<apex:form >
    <apex:pageblock >
        <apex:pageblockTable value="{!Groups}" var="c">
            <apex:column value="{!c.Name}"/>       
         </apex:pageblockTable>
    </apex:pageblock>
</apex:form>
</apex:page>


/*********************** Controller********************************/
public with sharing class Sample_Groups{

    public List<Group_Details__c> Groups{get;set;}

    public InvestorGroups()
    {
        Map<String,Group_Details__c> allGroups = Investor_Details__c.getAll();
        Groups = allGroups.values();
    } 

}
14
  • Some questions: Where are you inputting the new value? (Is there an input inputfield on the VF page?) And is your multiselect field a field on an object? Or is it only to be used in this VF page? Commented Mar 10, 2014 at 15:43
  • Yes.You are correct.new values need to save into custom settings and mullti-select pick list from object field.
    – user5257
    Commented Mar 10, 2014 at 15:49
  • Not sure if saving it into both the custom setting and the multi-select picklist field is a good idea.. Do you really need the 2nd option? Commented Mar 10, 2014 at 16:11
  • Yes I need second functionality.I am trying as follows.
    – user5257
    Commented Mar 20, 2014 at 14:10
  • public List<SelectOption> getMyOptions(){ List<SelectOption> options = new List<SelectOption>(); Schema.DescribeFieldResult fieldResult = customobject__c.custommultiselect__c.getDescribe(); List<Schema.PicklistEntry> picklistEntries = fieldResult.getPicklistValues(); for(Schema.PicklistEntry entry : picklistEntries){options.add(new SelectOption(entry.getValue(),entry.getLabel())); }for(Investor_Details__c setting : Investor_Details__c.getAll().values()){ options.add(new SelectOption(setting.name, setting.name)); }return options;
    – user5257
    Commented Mar 20, 2014 at 14:17

1 Answer 1

1

Custom setting objects can be created in apex just like a custom object.

setting_name__c settings = setting_name__c.getInstance();

Depending on they type of setting you are using you would need a different method to receive it. See: https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_methods_system_custom_settings.htm

Variables(fields) can then be set using standard functionality, ie:

settings.fieldName = localVar;

Combining this you can achieve something like:

public with sharing class Sample_Groups{

public List<Group_Details__c> Groups{get;set;}
public String localVar{get;set;}

public InvestorGroups()
{
    setting_name__c settings = setting_name__c.getInstance();

    //localVar = settings.fieldName 
    //picklistvalues = settings.values;

    Map<String,Group_Details__c> allGroups = Investor_Details__c.getAll();
    Groups = allGroups.values();
} 

public PageReference applySettings(){

    settings.fieldName = localVar;

     try{
        update settings;
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Confirm,
        'Settings Updated: '));
    }
    catch (Exception e) {
      ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.WARNING,
        'Failed to update settings' );
    }

    return null;
}}

Depending on what you want in the visualforce page you'll probably have an input field that uses {!localVar} to input the new value to save. You can get the values from the settings object to fill in the pick list field pretty easily as well, look into Select List and Select Options. Depending on how the Custom Setting is set up you'll have to use the values stored to create selectOption objects and add them to a List to populate the visualforce page.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .