+ Start a Discussion
sfg1sfg1 

Need to retrieve selected fields based on object selection dynamically

I am trying to display only selected fields based on selected object dynamically, Now i am able to display all fields but i need to display only seleted fields. Can anyone please guide me in this. Please find my below code.
Visualforce page:

<apex:page controller="Sample" >
<apex:form >
    <apex:pageBlock >
        <apex:pageBlockSection >
            <apex:pageBlockSectionItem >
                <apex:outputLabel value="Objects"/>
            </apex:pageBlockSectionItem>        
            <apex:pageBlockSectionItem >
                <apex:selectList size="1" value="{!obj}" >
                    <apex:selectOptions value="{!objs}"/>
                </apex:selectList>
            </apex:pageBlockSectionItem>                      
        </apex:pageBlockSection>
        <apex:pageblockButtons >
            <apex:commandButton value="Fetch Fields" action="{!fetchFields}" reRender="flds"/>
        </apex:pageblockButtons>
    </apex:pageBlock>
    <apex:pageblock id="flds" title="Fields">
        <apex:pageblockTable value="{!objFields}" var="f">
            <apex:column value="{!f}" />
        </apex:pageblockTable>
    </apex:pageblock>    
</apex:form>    
</apex:page> 

Apex controller:

public class Sample 

    public String obj;
    public List<String> objFields {get;set;}
    
    public Sample() 
    {    
    }
    public String getobj()
    {
        return obj;
    }
     public void setobj(String obj)
    {
        this.obj = obj;
    }   
    
    public List<SelectOption> getobjs()
    {
        List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();    
        List<SelectOption> options = new List<SelectOption>();
               
        for(Schema.SObjectType f : gd)
        {
            options.add(new SelectOption(f.getDescribe().getName(),f.getDescribe().getName()));
        }
        return options;
    }
  
    public void fetchFields()
    { 
        List<String> fields = new List<String>();
        Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
        System.debug('Selected Object is ' + obj);
        Schema.sObjectType objType = globalDescription.get(obj); 
        System.debug('Object Type is ' + objType);
        Schema.DescribeSObjectResult r1 = objType.getDescribe(); 
        
        Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();  

        for(Schema.SObjectField field : mapFieldList.values())  
        {  
            Schema.DescribeFieldResult fieldResult = field.getDescribe();  
            
            if(fieldResult.isAccessible())  
            {  
                System.debug('Field Name is ' + fieldResult.getName());
                fields.add(fieldResult.getName());
            }  
        }
        List<String> so = new List<String>();
        for(String f : fields)
        {
            so.add(f);
        } 
        objFields = so;       
    }   
}
 
Rahul KumarRahul Kumar (Salesforce Developers) 
Hi,
 
<apex:page controller="dynamicApex5">
 <apex:form id="theForm">
     <apex:pageBlock title="Select Object and Fields">
         <apex:pageBlockSection title="Select an Object" collapsible="false" columns="2">
             <apex:pageBlockSectionItem >
                 <apex:outputLabel value="Select an object:"></apex:outputLabel>
                 <apex:selectList value="{!objectName}" size="1" multiselect="false">
                     <apex:selectOptions value="{!selectedobject}">
                       
                     </apex:selectOptions>
                     <apex:actionSupport action="{!function}" event="onchange" reRender="fieldsPanel"/>  
                 </apex:selectList>
             </apex:pageBlockSectionItem>
             
             <apex:pageBlockSectionItem >
             <apex:outputPanel id="fieldsPanel">
                 <apex:outputLabel value="Select Fields:"> </apex:outputLabel>
                 <apex:selectList value="{!fields}" multiselect="true" size="3" >
                     <apex:selectOptions value="{!lsps}">
                     
                     </apex:selectOptions>
                 </apex:selectList>
                 </apex:outputPanel>
             </apex:pageBlockSectionItem>
            
             
         </apex:pageBlockSection>
     </apex:pageBlock>
 </apex:form>
</apex:page>
 
Controller

public class dynamicApex5 {

   public list<SelectOption> selectedobject { get; set; }

    public String objectName { get; set; }    
    public set<string> flds{get;set;}
    
    Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public dynamicApex5(){
                   
                selectedobject = new list<SelectOption>();  
                  selectedobject.add(new selectoption('','-None-'));
                  list<schema.SobjectType> lst=schemaMap.values();
                  for(schema.SobjectType ss:lst){
                      selectedobject.add(new selectoption(ss.getDescribe().getLocalName(),ss.getDescribe().getLabel()));
                      selectedobject.sort();
               }
                 flds= new set<string>();
                  lsps=new list<SelectOption>();
    }
 
      
      public String fields { get; set; }
      
       public list<SelectOption> lsps{get;set;}
      
       public void function() {
       
         if(objectName!=null || objectName!=''){
           Map<string,Schema.SObjectField> mpConField=schemaMap.get(objectName).getDescribe().fields.getMap();
               flds=mpConField.keySet();
                                
                   for(string name:flds){
                   Schema.DescribeFieldResult fldResult=mpConField.get(name).getDescribe();
                    lsps.add(new SelectOption(fldResult.getName(),fldResult.getName()));
                    lsps.sort();                    
                   }
                }        
    }
    
        

}

Please refer the below link for reference.
https://success.salesforce.com/answers?id=90630000000h1YfAAI

Hope it will be helpful.

Please mark it as best answer if the information is informative.so that question is removed from an unanswered question and appear as a proper solution.

Thanks
Rahul Kumar