+ Start a Discussion
ASHISH VARMAASHISH VARMA 

Display all Object,their fields and Dynamically fetch their records

I have developed a Visualforce page which contains two dropdownsWill show all Objects in my organisation.
​Will show all fields based on selection of objects.​I can multiselect fields from second dropdown, and a SELECT query is generated.
I have fetched all deatil records of those fields but I am not able to display those records on Visualforce Page as the data is Dynamic, every time you choose different object their corresponding fileds I need to display different columns,thier records. 
Please help me to sort this problem.


Visualforce Code:


<apex:page controller="CustomWorkbench">
  <apex:form >
     <apex:pageBlock >
         <apex:outputLabel value="Choose Object : "></apex:outputLabel>
                  <apex:selectList value="{!AllObjectsList}" size="1" onchange="change()">
                      <apex:selectOptions value="{!AllObjects}">
                      </apex:selectOptions>
                  </apex:selectList>
              
                  <apex:actionFunction name="change"  action="{!getAllFields}" reRender="fields1" status="Loading"/><br/>
                  <apex:actionStatus id="Loading" startText="Loading..."></apex:actionStatus>
    
     </apex:pageBlock>
     <apex:pageBlock id="fields1">
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <b><apex:OutputText value="Select {!AllObjectsList} Fields : "></apex:OutputText></b>
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <apex:selectList value="{!CorrespondingFields}"  size="5" multiselect="true"> 
             <apex:selectOptions value="{!fieldNames}" >
             </apex:selectOptions>
         </apex:selectList><br/><br/>
         &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        
         <apex:commandButton value="Generate Query" action="{!DisplayRecords}" status="Loading" reRender="showrecords,querytextarea"/><br/>
      
          &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
         <apex:inputTextarea disabled="true" value="{!query1}" rows="2"  cols="100" id="querytextarea"/>
     </apex:pageBlock>
     <apex:pageBlock id="showrecords" rendered="true">
         
        <!--<apex:pageBlockTable value="{!DisplayRecords}" var="fields">
            
             <apex:repeat value="{!fieldNameToString}" var="dynamicfields">
               <apex:column value="{!fields[dynamicfields]}" />
             </apex:repeat>
         </apex:pageBlockTable>-->
     </apex:pageBlock>
  </apex:form>
</apex:page>

Apex Code:

public with sharing class CustomWorkbench {

    public List<sObject> recordlist { get; set; }
    public String query1 { get; set; }
    public List<String> fieldNameToString { get; set; }
    public List<SelectOption> fieldNames{get; set;}
    public String CorrespondingFields{ get; set; }
    public String AllObjectsList { get; set; }
    
    public class innerWrapperClass{
        List<String> SelectedFieldsName { get; set; }
    }
       
    public PageReference DisplayRecords() {
        if(CorrespondingFields != null){
            integer length = CorrespondingFields.length();
            CorrespondingFields= CorrespondingFields.substring(1,length-1);
            String[] separate = CorrespondingFields.split(',\\s*');
            query1 = 'Select ' + CorrespondingFields + ' from ' + AllObjectsList;
            try{
                recordlist = Database.query(query1);
            }
            catch(Exception es){
                ApexPages.addMessages(es);
            }
            System.debug('recordlist ' + recordlist );
            System.debug('query1 '+query1 );
        }
        return null;
    }
    
    public void getAllFields() {
        System.debug('all AllObjectsList' + AllObjectsList);
        fieldNameToString = new List<String>();
        fieldNames = new List<SelectOption>();
        if(AllObjectsList != null){
            Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
            Schema.SObjectType ObjectSchema = schemaMap.get(AllObjectsList);
            Map<String, Schema.SObjectField> fieldMap = ObjectSchema.getDescribe().fields.getMap();
            
            for(String fieldName: fieldMap.keySet()) {  
                fieldNames.add(new SelectOption(fieldName,fieldName));
                fieldNameToString.add(fieldName);
                //fieldNameToString = (List<String>)fieldNames;
            }
        }
        System.debug('all fields' + fieldNames);
        System.debug('fieldNameToString' + fieldNameToString);
    }

    public List<SelectOption> getAllObjects() {
        List<Schema.sObjectType> gd = Schema.getGlobalDescribe().Values();
        List<SelectOption> options = new List<SelectOption>();
        options.add(new SelectOption('','--------Select--------'));
        for(Schema.sObjectType gd1 : gd){
            options.add(new SelectOption(gd1.getDescribe().getName(),gd1.getDescribe().getName()));
        }
        options.sort();
        return options;
    }

    /*public List<sObject> getDisplayRecords() {
        if(CorrespondingFields != null){
            integer length = CorrespondingFields.length();
            CorrespondingFields= CorrespondingFields.substring(1,length-1);
            String[] separate = CorrespondingFields.split(',\\s*');
            System.debug('separate :' + separate[0] );
            query1 = 'Select ' + CorrespondingFields + ' from ' + AllObjectsList;
            recordlist = Database.query(query1);
            System.debug('recordlist ' + recordlist );
            System.debug('query1 '+query1 );
        }
        return recordlist ;
    }*/
}
Shaijan ThomasShaijan Thomas
Execute the the Query and store in sObject and Display in VF page. 
ASHISH VARMAASHISH VARMA
I have alreay done it, query is executed and also I have stored all records in sObject. But i cant figure out how to display it in Visualforce page as Column Header differ always for different objects.
Subhasini Bhosal 6Subhasini Bhosal 6
Any idea on how to display two tables on a vf page and compare each row of the two tables and display the output as matched or not matched on click of a button ?In the second table there should be a flag column that should be updated with true or false if any of the rows from two table match..