+ Start a Discussion
nishad basha 7nishad basha 7 

How to display the standard object records once click search command button in visualforce page

if i select any standard object click on search button i want to display the related object records
Best Answer chosen by nishad basha 7
Keyur  ModiKeyur Modi
Hi SS,
<apex:page controller="Quetion1Controller">
    <apex:form >
        <apex:inputText value="{!selectedSobject}" />
        <apex:commandButton value="Search" action="{!doSearch}"/>
    </apex:form>
    <apex:pageBlock >
        <apex:pageblockTable value="{!lstQuery}" var="eachRecord">
            <apex:column value="{!eachRecord.id}"/>
            <apex:column value="{!eachRecord['Name']}"/>
        </apex:pageblockTable>
    </apex:pageBlock>
</apex:page>



Controller

public class Quetion1Controller {

public string selectedSobject{get;set;}
public List<Sobject> lstQuery{get;set;}

    public Quetion1Controller(){
        lstQuery=new List<Sobject>();
    }
    
    public Void doSearch(){
    
        string Query='';
        Query='SELECT id,Name FROM '+ selectedSobject;
        system.debug('==Query=='+Query);
        
        lstQuery=Database.query(Query);
        system.debug('==lstQuery=='+lstQuery);
    
    }
}

use this bunch of code it will help you.

All Answers

Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi Nishad,

Here are the steps for it.

1. Generate standars object and their corresponding fields in list of options dynamically in your controller.
2. Upon selection of object name and ,call your controller method to frame dynamic query.(Take care for each object's fields)
3. Execute query and display result.

Let us know if it helps you.
 
Keyur  ModiKeyur Modi
<apex:page controller="Quetion1Controller">
    <apex:form >
        <apex:selectList value="{!selectedSobject}" multiselect="false" size="1" >
            <apex:selectOption itemValue="Account" itemLabel="Accounts"/>
            <apex:selectOption itemValue="Contact" itemLabel="Contacts"/>
            <apex:selectOption itemValue="Opportunity" itemLabel="Opportunity"/>
        </apex:selectList>
        <apex:commandButton value="Search" action="{!doSearch}"/>
    </apex:form>
    <apex:pageBlock >
        <apex:pageblockTable value="{!lstQuery}" var="eachRecord">
            <apex:column value="{!eachRecord.id}"/>
            <apex:column value="{!eachRecord['Name']}"/>
        </apex:pageblockTable>
    </apex:pageBlock>
</apex:page>



Controller

public class Quetion1Controller {

public string selectedSobject{get;set;}
public List<Sobject> lstQuery{get;set;}

    public Quetion1Controller(){
        lstQuery=new List<Sobject>();
    }
    
    public Void doSearch(){
    
        string Query='';
        Query='SELECT id,Name FROM '+ selectedSobject;
        system.debug('==Query=='+Query);
        
        lstQuery=Database.query(Query);
        system.debug('==lstQuery=='+lstQuery);
    
    }
}

Use this bunch of code it will help you.

nishad basha 7nishad basha 7
Thank you very much
nishad basha 7nishad basha 7
Hi,  Ashish_Sharma_DEVSFDC

How to display the standard object records without use these  tag <apex:selectoptions> once click search button display the records  using   <apex:inputtext> tag.   please give some examples of that code.
 
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi nishad basha 7,

Keyur has mentioned that in above example.
<apex:pageBlock >

        <apex:pageblockTable value="{!lstQuery}" var="eachRecord">

            <apex:column value="{!eachRecord.id}"/>

            <apex:column value="{!eachRecord['Name']}"/>

        </apex:pageblockTable>

    </apex:pageBlock>

 
nishad basha 7nishad basha 7
please can you give me the example of that   <apex:inputtext> tag  once click search button display the records 
 
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC

Hi nishad basha 7

What you want to do ? Please paste your requirement .



 

S SaiS Sai
Hi Ashish 

<apex:selectList value="{!selectedSobject}" multiselect="false" size="1" >
<apex:selectOption itemValue="Account" itemLabel="Accounts"/>
 <apex:selectOption itemValue="Contact" itemLabel="Contacts"/>
<apex:selectOption itemValue="Opportunity" itemLabel="Opportunity"/>
</apex:selectList>
Don't Use This tag


U can Use 

<apex:inputText value="{!searchKeyword}" action = {!serch}/> when press the button then display the records
 
Ashish_Sharma_DEVSFDCAshish_Sharma_DEVSFDC
Hi,

Sure we can use inputSelect in above case ,but you have to enter object name correctly everytime.
so better to use picklist.

Hope this helps.
S SaiS Sai
But My Requuirment is only in using in <apex:inputtext > tag
Keyur  ModiKeyur Modi
Hi SS,
<apex:page controller="Quetion1Controller">
    <apex:form >
        <apex:inputText value="{!selectedSobject}" />
        <apex:commandButton value="Search" action="{!doSearch}"/>
    </apex:form>
    <apex:pageBlock >
        <apex:pageblockTable value="{!lstQuery}" var="eachRecord">
            <apex:column value="{!eachRecord.id}"/>
            <apex:column value="{!eachRecord['Name']}"/>
        </apex:pageblockTable>
    </apex:pageBlock>
</apex:page>



Controller

public class Quetion1Controller {

public string selectedSobject{get;set;}
public List<Sobject> lstQuery{get;set;}

    public Quetion1Controller(){
        lstQuery=new List<Sobject>();
    }
    
    public Void doSearch(){
    
        string Query='';
        Query='SELECT id,Name FROM '+ selectedSobject;
        system.debug('==Query=='+Query);
        
        lstQuery=Database.query(Query);
        system.debug('==lstQuery=='+lstQuery);
    
    }
}

use this bunch of code it will help you.

This was selected as the best answer
S SaiS Sai
 hi Keyur Modi

i want to diaplys how meany records are founded in the same requerment 

thank u so much
 
S SaiS Sai
to display when  i am serching each object in that time i want to show how many records are founded 
Keyur  ModiKeyur Modi
Hi ss,
Use this bunch of code: 
<apex:page controller="Quetion1Controller">
    <apex:form >
        <apex:inputText value="{!selectedSobject}" />
        <apex:commandButton value="Search" action="{!doSearch}"/>
    </apex:form>
Total Records : {!Total}
    <apex:pageBlock >
        <apex:pageblockTable value="{!lstQuery}" var="eachRecord">
            <apex:column value="{!eachRecord.id}"/>
            <apex:column value="{!eachRecord['Name']}"/>
        </apex:pageblockTable>
    </apex:pageBlock>
</apex:page>

Controller

public class Quetion1Controller {

public string selectedSobject{get;set;}
public List<Sobject> lstQuery{get;set;}
public integer Total{get;set;}
    
   public Quetion1Controller(){
        lstQuery=new List<Sobject>();
    }
    
    public Void doSearch(){
    
        string Query='';
        Query='SELECT id,Name FROM '+ selectedSobject;
        system.debug('==Query=='+Query);
        
        lstQuery=Database.query(Query);
       Total=lstQuery.size();
       system.debug('==lstQuery=='+lstQuery);
    
    }
}

If this code will help then like this comment and select Answer as best answear in below link so everyone can get the answer easily.
https://developer.salesforce.com/forums?id=906F0000000BCFpIAO. This link is posted by you so in that also i posted the same answer so don't forget to select that asnwer as best answer.
S SaiS Sai
Thank's Keyur Modi Ur The Best
nishad basha 7nishad basha 7
Hi, Keyur Modi

                   How to run the user require fields for Test class in salesforce. i want to show percentage code coverage. please give some ideas with example code.
Keyur  ModiKeyur Modi

Hi Nishad basha 7,

You can use this link https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods where you can learn how to write test class , with basic example , i hope that will help you 

Thanks,

nishad basha 7nishad basha 7

Hi, Keyur Modi 
      
                 How to run the userdetails with all require fields in test class.But i want  to show the code coverage of percentage  that Apex class. how that one can give me one example for that.
S SaiS Sai
Hi Keyur Modi
Keyur  ModiKeyur Modi

Hi nishad basha 7,

write the test class with help of this link https://developer.salesforce.com/page/An_Introduction_to_Apex_Code_Test_Methods nad save the class after saving the class there is one button called Run Test if you click on that it will run the test class and after that it will show the code coverage to original class.

Still if you found dificulty then feel free to ask.

S SaiS Sai
HI Keyur Modi

<apex:page controller="Quetion1Controller">
<apex:form >
<apex:inputText value="{!selectedSobject}" />
<apex:commandButton value="Search" action="{!doSearch}"/>
</apex:form> Total Records : {!Total} <apex:pageBlock >
<apex:pageblockTable value="{!lstQuery}" var="eachRecord">
<apex:column value="{!eachRecord.id}"/> <apex:column value="{!eachRecord['Name']}"/>
</apex:pageblockTable>
</apex:pageBlock>
</apex:page>


Controller public class Quetion1Controller {
public string selectedSobject{get;set;}
public List<Sobject> lstQuery{get;set;}
public integer Total{get;set;}
public Quetion1Controller(){
lstQuery=new List<Sobject>();
}
public Void doSearch(){
string Query=''; Query='SELECT id,Name FROM '+ selectedSobject; system.debug('==Query=='+Query);
lstQuery=Database.query(Query);
Total=lstQuery.size();
system.debug('==lstQuery=='+lstQuery);
}
}


In This Code I want tothrow the custome error and ine more thing here i want to dispaly case records also...... plz solve the problem
Keyur  ModiKeyur Modi

Hi SS,
FOr the custom error you can use <apex:pageMessage> tag and for the case object record you need to change the string  Query.Because Name Filed is not there in case.

string Query=''; Query='SELECT id,CaseNumber FROM '+ selectedSobject; system.debug('==Query=='+Query);

still if you found any dificulty then feel free to ask.

S SaiS Sai
Hi Keyur Modi 

wehn i am serching any object in the serch it show's the object lerated records 
 for ex:- now i am serching account object it show's account records

now i am serching case object it throw error 




 
nishad basha 7nishad basha 7

Hi ,Keyur Modi 

how to display the once click search button custom error message will show in same page
Keyur  ModiKeyur Modi

Hi SS,

in clontroller you can check like 

if(selectedSobject=='Case'){
Query='SELECT id,CaseNumber FROM '+ selectedSobject; system.debug('==Query=='+Query);
}
else{
Query='SELECT id,Name FROM '+ selectedSobject; system.debug('==Query=='+Query);
}

still if you found dificulty feel free to ask.

Keyur  ModiKeyur Modi
Hi nishad basha 7,
Can you give me the clear idea about what exactly you are asking for. With your above quetion i am not able to understand your quetion
S SaiS Sai
Hi Keyur modi It,s Not Working

if(selectedSobject=='Case'){
Query='SELECT id,CaseNumber FROM '+ selectedSobject; system.debug('==Query=='+Query);
}
else{
Query='SELECT id,Name FROM '+ selectedSobject; system.debug('==Query=='+Query);
}
S SaiS Sai
Hi Keyur Modi,

ur giving the code is not working 
 
S SaiS Sai
Hi Keyur Modi 


f(selectedSobject=='Case'){
Query='SELECT id,CaseNumber FROM '+ selectedSobject; system.debug('==Query=='+Query);
}
else{
Query='SELECT id,Name FROM '+ selectedSobject; system.debug('==Query=='+Query);
}

This is Not working  I Traied It ......
 
Keyur  ModiKeyur Modi
Hi SS,
<apex:page controller="Quetion1Controller">
    <apex:form >
        <apex:inputText value="{!selectedSobject}" />
        <apex:commandButton value="Search" action="{!doSearch}"/>
    </apex:form>
    Total Records : {!Total}
    <apex:pageMessages />
    <apex:pageBlock >
      <apex:outputPanel id="outputpanelId" rendered="{!NOT(flag)}">
        <apex:pageblockTable value="{!lstQuery}" var="eachRecord">
            <apex:column value="{!eachRecord.id}"/>
            <apex:column value="{!eachRecord['Name']}"/>
        </apex:pageblockTable>
      </apex:outputPanel>
      <apex:outputPanel id="outputpanelCaseId" rendered="{!flag}">
        <apex:pageblockTable value="{!lstQuery}" var="eachRecord">
            <apex:column value="{!eachRecord.id}"/>
            <apex:column value="{!eachRecord['CaseNumber']}"/>
        </apex:pageblockTable>
      </apex:outputPanel>
    </apex:pageBlock>
</apex:page>

controller

public class Quetion1Controller {

public string selectedSobject{get;set;}
public List<Sobject> lstQuery{get;set;}
public integer Total{get;set;}
public boolean flag{get;set;}
    
   public Quetion1Controller(){
        lstQuery=new List<Sobject>();
        flag=false;
    }
    
    public Void doSearch(){
        string Query='';
        system.debug('==value=='+selectedSobject.equalsIgnoreCase('case'));
        
        if(selectedSobject.equalsIgnoreCase('case')){
        flag=true;
        Query='SELECT id,CaseNumber FROM '+ selectedSobject;
        system.debug('==Query case=='+Query);
        }
        else
        {
        flag=false;
        Query='SELECT id,Name FROM '+ selectedSobject;
        system.debug('==Query=='+Query);
        }
        try{
        lstQuery=Database.query(Query);
        Total=lstQuery.size();
        system.debug('==lstQuery=='+lstQuery);
        }
        catch(exception e){
         Apexpages.addMessages(e);
        }    
    }
}

use this code this code it will help you in your requirement. still you found any dificulty then feel free to ask.
Keyur  ModiKeyur Modi
Hi nishad basha 7,

I think you want to display custom error in visual force page. please reffer above comment , try that above code it will help you in your requirment ,in that code there is custom error displayed . if you try that code then in that code if you enter wong object name then it will display error message.
still you have any doubt then feel free to ask.
S SaiS Sai
Hi Keyur Modi 
Thank U soo Much
 
S SaiS Sai
Hi Keyur Modi

when iam searching case that time show data correct but after i am enter another object that time show the error User-added image
Here Showing Correct

when i am entering object name wrong  that time throwing system error
User-added image
I Dont Want Show This error Here

 
S SaiS Sai
Only It Showing when i am entering case after entering caseaa
 
Keyur  ModiKeyur Modi
Hi SS,

Due to this issue you only Ashish_Sharma_DEVSFDC suggest you to use picklist.
nishad basha 7nishad basha 7
Hi, Keyur Modi

how to display Account object labels using visualforce page? how to solve the solution please give any ideas.
nishad basha 7nishad basha 7

Hi, Keyur Modi

 my requirement u must have one  require field,when i click the save (don't fill the values) it will show custom error message
nishad basha 7nishad basha 7
Hi, Dhaval Panchal

page:
<apex:page controller="Stall1" tabStyle="Stall__c" sidebar="false">
  <apex:form >
  <apex:pageBlock mode="edit" title="enter stall detail">
    <apex:pageBlockButtons location="bottom">
    <apex:pageMessages ></apex:pageMessages>
  <apex:commandButton action="{!Save}" value="Save"/>
</apex:pageBlockButtons>
  <apex:pageBlockSection columns="1">
  <apex:inputField value="{!Book__c}" required="true"/>
    
  </apex:pageBlockSection> 
  
  </apex:pageBlock>
  </apex:form>  
</apex:page>
class:
public with sharing class Stall1 {

    public String getStall() {
        return null;
    }

Public Stall__c stall;
public pageReference save(){
        
    ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter value'));
    return null;

 

}

here iam getting the these type of error: Could not resolve the entity from <apex:inputField> value binding '{!Book__c}'. <apex:inputField> can only be used with SObjects, or objects that are Visualforce field component resolvable
How to solve the above scenario please give some ideas.
nishad basha 7nishad basha 7
Hi, Ashish_Sharma_DEVSFDC

         How to retrieve the table format Account of contacts using visualforce page?
 i want to display the  number of Accounts having contact records. how to solve above scenario please give some ideas.