Managing Custom setting records on visualforce page

Hi you all know about the use of 'Custom setting' in the Force.com 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.

Idea: 
    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'.

Solution:
      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.

Demo:
       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:actionstatus></apex:pageblocksection>
 
       </apex:pageblock>
 
       
 
       <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:pagemessage></apex:outputpanel>
 
       
 
       <apex:outputpanel id="theValBlock">
 
      
 
       <apex:pageblock rendered="{!AND(cusSetObj!='--Select--',cusSetObj!='')}" title="List of records">
 
            <apex:pagemessages>
 
            <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:param></apex:commandlink>
 
                        <apex:variable value="{!index+1}" var="index">
 
                   </apex:variable></apex:column>
 
                 
 
                   <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:outputfield></apex:inputfield></apex:column> 
 
                </apex:repeat>
 
          </apex:pageblocktable>
 
       </apex:variable></apex:pageblocksection> 
 
       
 
          <apex:pageblockbuttons>
 
                  <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:commandbutton></apex:commandbutton></apex:commandbutton></apex:commandbutton></apex:pageblockbuttons>
 
       </apex:pagemessage></apex:pagemessages></apex:pageblock>
 
        </apex:outputpanel>
 
 </apex:form>
 
</apex:page>

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;
                                     }set;}  
        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(){
                if(cusSetObjVal.isEmpty()){
                        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(){
               if(cusSetObj!='--Select--'){
                    recordList();
                      
               }
               else{
                   errorMsg=false;
               }
              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>();
             records.clear();
             string fieldResult= '';
             fieldResult=ObjectAPINameFields(cusSetObj);     
             String qryString = 'Select '+fieldResult+' from'+' '+cusSetObj +' '+'Limit 100';
             records=Database.query(qryString);
              
             if(records.isEmpty()){
                //Setting true for if there is no records for selected object
                 errorMsg=true;
             }
             else{
                 errorMsg=false;
             }
              
             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){
               SelectedFields.Clear();
               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'){
                        SelectedFields.add(fieldAPI.getDescribe().getName());
                    }
                     
                    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(){
               try{
                   editAll=true; 
               }
               catch(Exception e){
                   System.debug('&&&&&'+e);
               }
                return null;
        }
       //END: EditAll Button action        
        
       //BEGIN: Cancel Edit Button action
        public pageReference CancelEditAllCustVal(){
               try{
                   getValues();
                   editAll=false; 
               }
               catch(Exception e){
                   System.debug('&&&&&'+e);
               }
                return null;
        }
       //END: Cancel Edit Button action
         
        //BEGIN: Delete Link action
        public pageReference DeleteCustVal(){
               try{
                    
                   integer i=Integer.ValueOf(recordIndex);
                   if(records.get(i).id!=null){
                       sobject delobj=records.get(i);
                       delete delobj;
                   }
                   else{
                       records.remove(i);
                   }
                   //getValues();
               }
               catch(Exception e){
                   System.debug('&&&&&'+e);
               }
                return null;
        }
        //END: Delete Link action
         
         //BEGIN: Saving All the custom setting both new and edit
        public pageReference SaveAll(){
               try{
                    Upsert records;
                    editAll=false;
               }
               catch(Exception e){
                   System.debug('&&&&&'+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();
                if(errorMsg==true){
                    errorMsg=false;
                }
                editAll=true;
                records.add(s);
                
               return null;
       }
       //BEGIN: creating new records for selected object
          
}
</string></sobject></sobject></selectoption></string></string></sobject></sobject></string></string></selectoption></selectoption>

 

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.

Rating: 
No votes yet