[SalesForce] Regarding VF page to custom settings

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();
    } 

}

Best Answer

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.

Related Topic