+ Start a Discussion
AHAMMEDAHAMMED 

In Vf Page

HI 

create one inputtext field and one submit button in that time i am entering any sobject in inputtext field and clicking submit button then i want to display enter sobject related data........
Best Answer chosen by AHAMMED
Pradeep Kumar L.GPradeep Kumar L.G
Ahammed,

Here you go, i have created basic skeleton of the code
1. Create Apex class name=QuerySobject and copy paste the code.
2. Create VF page and copy paste the code.
3. Select object from dropdown and click on button.

VF Page Code:
<apex:page showHeader="true" controller="QuerySobject">
    <apex:form >
        <apex:actionstatus id="progress" startText=" (please wait...)"/>  
        <b>Select Object</b><apex:selectList value="{!selectObject}" multiselect="false" size="1">  
        <apex:selectOptions value="{!ObjectNames}"/>  
        </apex:selectList>
        <apex:commandButton value="Query For Records" action="{!queryforRecords}" reRender="pbId,pbsectionId"/>  
        <apex:pageBlock id="pbId">
            <apex:pageBlockSection id="pbsectionId">
                <apex:pageblockTable value="{!sobjectList}" var="sobjectRecord">
                    <apex:column value="{!sobjectRecord.ID}"/>
                </apex:pageblockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>

Apex Class Code.
public class QuerySobject {

    public List<Sobject> sobjectList{get;set;}
      private Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();  
      public String selectedObject {get; set;}  
      public List<String> entities{get; set;}  
      public List<SelectOption> objNames{get;set;}  
      public String SelectObject{get;set;}  
      
      public List<SelectOption> getObjectNames(){  
          objNames = new List<SelectOption>();  
           entities = new List<String>(schemaMap.keySet());  
           entities.sort();  
           objNames.add(new SelectOption('None','--Select Object--'));  
           for(String name :entities)        
            {  
                objNames.add(new SelectOption(name,name));  
            }         
           return objNames;         
      } 
    public void queryforRecords(){
          sobjectList=new List<Sobject>();
            String queryString='select id,Name from '+selectObject;
            if(String.isNotBlank(selectObject)){
              sobjectList=Database.query(queryString);
              }
    
      }
}


Thanks
Pradeep

Mark this answer solved if this resolve your problem.

All Answers

Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Shaik,

On button click call apex controller method and use dynamic apex.
Use below link to use dynamic apex.
https://www.salesforce.com/us/developer/docs/apexcode/Content/apex_dynamic_describe_objects_understanding.htm.

Just a suggestion ,use picklist to select sObject rather than using an nputTextField.
 
Pradeep Kumar L.GPradeep Kumar L.G
Check this "http://sforcetech.blogspot.com.au/2013/03/describe-objects-and-related-fields.html" and Demo :"http://lgdemosite-developer-edition.ap1.force.com/DescribeObjectPage.

Thanks
Pradeep
 
Pradeep Kumar L.GPradeep Kumar L.G
Hi Ahammed,

You can find the full code in the blog. But below code is all you need, use this as reference.

//Code to get all the SObjects
private Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe(); 
public String selectedObject {get; set;}
public List<String> entities{get; set;}
public List<SelectOption> objNames{get;set;}

public List<SelectOption> getObjectNames(){  
           objNames = new List<SelectOption>();  
           entities = new List<String>(schemaMap.keySet());  
           entities.sort();  
           objNames.add(new SelectOption('None','--Select Object--'));  
           for(String name :entities)        
            {  
                objNames.add(new SelectOption(name,name));  
            }         
           return objNames;         
      }
      
Public List<Sobject> queryForSelectedObject(){
// Your query logic goes here. Make use of dynamic apex to construct the query based on selected object from the dropdown

}

VF page Code:
<b>Select Object</b><apex:selectList value="{!selectObject}" multiselect="false" size="1"> <apex:selectOptions value="{!ObjectNames}"/>


Thanks
Pradeep

Mark this answer solved if this resolve your problem.
Pradeep Kumar L.GPradeep Kumar L.G
Ahammed,

Here you go, i have created basic skeleton of the code
1. Create Apex class name=QuerySobject and copy paste the code.
2. Create VF page and copy paste the code.
3. Select object from dropdown and click on button.

VF Page Code:
<apex:page showHeader="true" controller="QuerySobject">
    <apex:form >
        <apex:actionstatus id="progress" startText=" (please wait...)"/>  
        <b>Select Object</b><apex:selectList value="{!selectObject}" multiselect="false" size="1">  
        <apex:selectOptions value="{!ObjectNames}"/>  
        </apex:selectList>
        <apex:commandButton value="Query For Records" action="{!queryforRecords}" reRender="pbId,pbsectionId"/>  
        <apex:pageBlock id="pbId">
            <apex:pageBlockSection id="pbsectionId">
                <apex:pageblockTable value="{!sobjectList}" var="sobjectRecord">
                    <apex:column value="{!sobjectRecord.ID}"/>
                </apex:pageblockTable>
            </apex:pageBlockSection>
        </apex:pageBlock>
        
    </apex:form>
</apex:page>

Apex Class Code.
public class QuerySobject {

    public List<Sobject> sobjectList{get;set;}
      private Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();  
      public String selectedObject {get; set;}  
      public List<String> entities{get; set;}  
      public List<SelectOption> objNames{get;set;}  
      public String SelectObject{get;set;}  
      
      public List<SelectOption> getObjectNames(){  
          objNames = new List<SelectOption>();  
           entities = new List<String>(schemaMap.keySet());  
           entities.sort();  
           objNames.add(new SelectOption('None','--Select Object--'));  
           for(String name :entities)        
            {  
                objNames.add(new SelectOption(name,name));  
            }         
           return objNames;         
      } 
    public void queryforRecords(){
          sobjectList=new List<Sobject>();
            String queryString='select id,Name from '+selectObject;
            if(String.isNotBlank(selectObject)){
              sobjectList=Database.query(queryString);
              }
    
      }
}


Thanks
Pradeep

Mark this answer solved if this resolve your problem.
This was selected as the best answer