Managing Custom setting records on visualforce page

Hi you all know about the use of 'Custom setting' in the platform.Today while I was doing my development, I have got one good idea. I hope this idea will be more useful for all to manage the List type 'Custom setting' object records.

    For assumption In my product I am using only List type custom setting object.I want to install my product to one of my clients. In this org already there is a list of apps and they may use some custom setting to run the app.
    For our app the system admin should know our app 'Custom Setting' Name and record name or  every time he need to  refer our 'Training Guide'. Even through the standard 'Custom setting ' Manage page is not much user friendly for clients.
    So, Its very headache work for 'System Admin'.

      To manage the List type 'Custom Setting' records, why can't we use Visualforce with more useful functionality like to create , edit and delete records in a single Visulaforce page.
      For security purpose we should create Visualforce tab and visible permission to our App and System Administrator.

       To give a demo I created 2 custom setting objects and one custom field for each objct
         1. Page Setting
                 1.1. State(Checkbox)
         2. PageBlock Section Setting
                  2.1. Profile (Text-Length 255)

        And also I created one Visualforce page 'manageCustomSetting' and controller called 'manageCustomSetting'.

Visualforce code:

<apex:page controller="manageCustomSettingCont" sidebar="false" title="Manage Custom Setting">
<apex:form id="frm">
       <apex:pageblock title="List of custom setting object">
          <apex:pagemessages id="errmsg"></apex:pagemessages>
            <apex:pageblocksection columns="1">
              <apex:selectlist label="Select object" size="1" value="{!cusSetObj}">
                    <apex:actionsupport action="{!getValues}" event="onchange" rerender="theValBlock,panl" status="recordstatus">
                   <apex:selectoptions value="{!cusSetObjVal}">
               </apex:selectoptions></apex:actionsupport></apex:selectlist><apex:actionstatus id="recordstatus" starttext="Loading Please wait....">
       <apex:outputpanel id="panl">
       <apex:pagemessage id="msg" rendered="{!OR(cusSetObj='--Select--',cusSetObj='')}" severity="info" strength="3" summary="Please Select the Custom setting object from the drop down to see list of records">
       <apex:outputpanel id="theValBlock">
       <apex:pageblock rendered="{!AND(cusSetObj!='--Select--',cusSetObj!='')}" title="List of records">
            <apex:pagemessage rendered="{!errorMsg}" severity="info" strength="3" summary="Sorry there are no records in database for selected object">
           <apex:pageblocksection columns="1">
              <apex:variable value="{!0}" var="index">
           <apex:pageblocktable value="{!records}" var="recList">
                   <apex:column headervalue="Action">
                       <apex:commandlink action="{!DeleteCustVal}" rerender="frm" value="Delete">
                              <apex:param assignto="{!recordIndex}" name="rcdind" value="{!index}">
                        <apex:variable value="{!index+1}" var="index">
                   <apex:repeat value="{!SelectedFields}" var="FieldLable">
                    <apex:column headervalue="{!FieldLable}">
                       <apex:inputfield rendered="{!editAll}" value="{!recList[FieldLable]}">
                       <apex:outputfield rendered="{!editAll!=true}" value="{!recList[FieldLable]}">
                  <apex:commandbutton action="{!addNewCutValue}" rendered="{!OR(editAll,errorMsg)}" value="Add New Value">
                   <apex:commandbutton action="{!EditAllCustVal}" rendered="{!AND(errorMsg!=true,editAll!=true)}" rerender="theValBlock" value="Edit All">
                  <apex:commandbutton action="{!SaveAll}" rendered="{!errorMsg!=true}" value="Save All">
                   <apex:commandbutton action="{!CancelEditAllCustVal}" immediate="true" rendered="{!AND(errorMsg!=true,editAll)}" value="Cancel Edit or New">               

Apex Code: 

public class manageCustomSettingCont {
        //BEGIN:Variable Declaration
         public boolean editAll{get;set;}
         public String recordIndex{get;set;}
         public boolean errorMsg{get;set;}
         public String cusSetObj{get;set;}
         public List<selectoption> cusSetObjVal=new List<selectoption>();
         public List<string> SelectedFields {get{
                                     if(SelectedFields ==null){
                                     SelectedFields =new List<string>();
                                     return SelectedFields;
        public List<sobject> records{get;set;}
        public List<sobject> recordsNew{get;set;}
        Map<string schema.sobjecttype=""> gd = Schema.getGlobalDescribe();  
        Set<string> objectKeys = gd.keySet();  
        //END:Variable Declaration
        //BEGIN : List of custom setting object added by user
        public  List<selectoption> getcusSetObjVal(){
                        cusSetObjVal.add(new selectoption('--Select--','--Select--'));
                        cusSetObjVal.add(new selectoption('PageBlock_Section_Setting__c','PageBlock Section Setting'));
                        cusSetObjVal.add(new selectoption('Page_Setting__c','Page Setting'));
                return cusSetObjVal;
        //END: List of custom setting object added by user
        //BEGIN: onchage action method for selection custom setting object from drop down
        public pageReference getValues(){
              return null;
        //END: onchage action method for selection custom setting object from drop down
        //BEGIN: Collecting the object records
        public List<sobject> recordList(){
             records=new List<sobject>();
             string fieldResult= '';
             String qryString = 'Select '+fieldResult+' from'+' '+cusSetObj +' '+'Limit 100';
                //Setting true for if there is no records for selected object
             return records;
       //END: Collecting the object records
        //BEGIN: Collecting the object fields API Name for query and for input field and output field
        public String ObjectAPINameFields(String ObjNameval){
               string fieldsAPIName = '';
                Schema.SObjectType systemObjectType = gd.get(cusSetObj);  
                Schema.DescribeSObjectResult r = systemObjectType.getDescribe();  
                Map<string schema.sobjectfield=""> M = r.fields.getMap();  
                for(Schema.SObjectField fieldAPI : M.values())  
                    if(fieldAPI.getDescribe().getName()!='Id' && fieldAPI.getDescribe().getName()!='SetupOwnerId' && fieldAPI.getDescribe().getName()!='IsDeleted' && fieldAPI.getDescribe().getName()!='CreateIsDeleted' && fieldAPI.getDescribe().getName()!='CreatedDate' && fieldAPI.getDescribe().getName()!='SystemModstamp' && fieldAPI.getDescribe().getName()!='LastModifiedById' && fieldAPI.getDescribe().getName()!='CreatedById' && fieldAPI.getDescribe().getName()!='LastModifiedDate'){
                    fieldsAPIName += fieldAPI+',';
                if(fieldsAPIName .endsWith(',')){
                   fieldsAPIName = fieldsAPIName .substring(0,fieldsAPIName.length()-1);
                return fieldsAPIName ;
        //END: Collecting the object fields API Name for query and for input field and output field
        //BEGIN: EditAll Button action
        public pageReference EditAllCustVal(){
               catch(Exception e){
                return null;
       //END: EditAll Button action        
       //BEGIN: Cancel Edit Button action
        public pageReference CancelEditAllCustVal(){
               catch(Exception e){
                return null;
       //END: Cancel Edit Button action
        //BEGIN: Delete Link action
        public pageReference DeleteCustVal(){
                   integer i=Integer.ValueOf(recordIndex);
                       sobject delobj=records.get(i);
                       delete delobj;
               catch(Exception e){
                return null;
        //END: Delete Link action
         //BEGIN: Saving All the custom setting both new and edit
        public pageReference SaveAll(){
                    Upsert records;
               catch(Exception e){
                return null;
         //END: Saving All the custom setting both new and edit
        //BEGIN: creating new records for selected object
       public pageReference addNewCutValue(){
               Schema.SObjectType t  = Schema.getGlobalDescribe().get(cusSetObj);
               SObject s = t.newSObject();
               return null;
       //BEGIN: creating new records for selected object


Screen Shot:

1. Initial page:

2. Selected 'Page Setting' from drop down:

3. After clicking 'Add New Value' button:

4. After Clicking 'Save All' Button:

5. Result in object level:

6. Adding more value in the existing list:

In this there is more functionality like update and delete the existing values. For search the value
you can use the standard functionality Ctrl+F.

I hope this will be very useful for all.

No votes yet