+ Start a Discussion
nishad basha 7nishad basha 7 

once click search button and select query button Dynamic SOQL query to fetch all fields in visualforce page?

select any object and click query button and  display Dynamic SOQL query to fetch all fields in table format.How to solve the above scenario please say examples on that.
Pankaj_GanwaniPankaj_Ganwani
Hi nishad,

You will have to make use of describe call for that, Please refer below mentioned code:

vf page......
<apex:page controller="FetchObjects"> <apex:sectionHeader title="Show All objects"></apex:sectionHeader> <apex:form > <apex:pageBlock title="Object Selection" mode="edit"> <apex:pageMessages id="ShowMessages"/> <apex:pageBlockSection title="Select your option" columns="1"> <apex:selectList value="{!selectedObject}" size="1"> <apex:actionSupport event="onchange" action="{!getFields}" /> <apex:selectOptions value="{!lstOptions}" /> </apex:selectList> </apex:pageBlockSection> <apex:pageBlockSection title="related Fields" rendered="{!AND(NOT(ISNULL(lstfieldname)),lstfieldname.size > 0)}" columns="1"> <apex:commandButton value="Show All Records" action="{!getRecords}" rerender="ShowRecords,ShowMessages"/> <apex:selectList multiselect="true" size="7" value="{!lstSelectedFields}"> <apex:selectOptions value="{!lstfieldname}"/> </apex:selectList> </apex:pageBlockSection> <apex:outputPanel id="ShowRecords"> <apex:pageBlockSection title="related records" columns="1" rendered="{!AND(NOT(ISNULL(lstObject)),lstObject.size > 0)}"> <apex:pageBlockTable value="{!lstObject}" var="obj"> <apex:repeat value="{!lstSelectedFields}" var="FieldName"> <apex:column value="{!obj[FieldName]}"/> </apex:repeat> </apex:pageBlockTable> </apex:pageBlockSection> </apex:outputPanel> </apex:pageBlock> </apex:form> </apex:page>
Controller...
public with sharing class FetchObjects 
{
    public list<Selectoption> lstOptions {get;set;}
    public String selectedObject{get;set;}
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public List<Selectoption> lstfieldname{get;set;}
    public List<Sobject> lstObject{get;set;}
    public List<String> lstSelectedFields{get;set;}
    public TBN_FetchObjects()
    {
        try
        {
            lstOptions = new List<Selectoption>();
            lstfieldname=new List<Selectoption>();
            lstObject=new List<Sobject>();
            lstOptions.add(new Selectoption('','--None--'));
            for(Schema.Sobjecttype objSobject: schemaMap.values())
            {           
                if(objSobject.getDescribe().isQueryable())          
                    lstOptions.add(new Selectoption(objSobject.getDescribe().getname(),objSobject.getDescribe().getLabel()));
            }
            lstOptions.sort();
        }
        catch(Exception e)
        {
            
        }
    }

    public void getFields()
    {
        try
        {
            lstfieldname.clear();
            Map<String,schema.Sobjectfield> mapFields=schemaMap.get(selectedObject).getDescribe().fields.getMap();     
            for(schema.Sobjectfield objSobjectFeild:mapFields.values())
                lstfieldname.add(new Selectoption(objSobjectFeild.getDescribe().getname(),objSobjectFeild.getDescribe().getLabel ()));
            
            lstfieldname.sort();
        }
        catch(Exception e)
        {
            
        }
    }
    
    public void getRecords()
    {
        try
        {
            lstObject.clear();
            String strQuery = 'Select Id ' ;            
            for(String field : lstSelectedFields)  
            {  
                //if(field.toLowerCase() != 'id' && field.toLowerCase() != '--None--')  
                strQuery += ','+ field + ' ' ;  
            }           
            strQuery += ' from ' + selectedObject+' LIMIT 5' ;    
            lstObject = Database.query(strQuery) ;
            if(lstObject.isEmpty())
                Apexpages.addMessage(new Apexpages.Message(ApexPages.Severity.INFO,'There is No Records!!'));
        } 
        catch(Exception e)
        {
            list<Period> lstPeriods=new List<Period>();
            lstPeriods=[Select p.Type,p.StartDate,p.Id,p.EndDate From Period p];
            system.debug('******'+lstPeriods);
            //system.debug('&&&&&&&'+Opportunity.SObjectType.getDescribe().isMergeable());
        }
    }
}

Please use this code. It may help you.