function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Not getting the desired output - plz help me out

Output should be like this..


if we select DeptName from dropdown then all the details(deptno,loc) of that particular "dept name" should be displyed by quering the records from the "custom object"....


in the below code, "dept" is the custom object..


I tried like this  but didnt get the required output.




public class DeptDropDownCon
   string dept;

public List<selectoption> getItems(){

List<selectoption> dept = new List<selectoption>();
dept.add(new selectoption('SPORTS','SPORTS'));
dept.add(new selectoption('SALES','SALES'));

return dept;

      public PageReference test() {
            return null;

public void setDept(string dept)


public List<dept__c> getDept()
   return[select name,loc__c from dept__c];







<apex:page controller="DeptDropDownCon">

      <apex:form >

                  <apex:selectList value="{!dept}" size="1">
                       <apex:selectOptions value="{!items}"/>


                 <apex:commandButton value="Test" action="{!test}" rerender="out" status="status"/>



<apex:outputPanel id="out">
         <apex:actionstatus id="status" startText="testing...">
                  <apex:facet name="stop">
<apex:outputPanel >
        <p>You have selected:</p>

<apex:dataTable value="{!dept}" var="d" width="50%" >
             <apex:column VAlue="{!d.ID}" headerValue="dept ID"/>
                    <apex:column VAlue="{!}" headerValue="dept Name"/>
                       <apex:column VAlue="{!d.LOC__c}" headerValue="dept location"/>
                             <!-- <apex:column VAlue="{!d.DEPTNO__c}" headerValue="dept no"/> -->

<!-- <apex:outputText value="{!dept}"/> -->






with the above controller and the VF page,  the same dept details will be displayed for all deptnames before click on test(immediately after saving).. 


dept details should be displayed only after I select the "deptname" and click on test..


I will be waiting for ur reply..




Best Answer chosen by Admin (Salesforce Developers) 



just replace the line-35 with following statement -


return [select name,loc__c from Dept__c where name =: dept];

All Answers




I guess, you forgot to add filter in your query, so it returns every time same records.



Thank u for ur valuable reply.


But here I want to display the required specific dept details only when I select that particular dept in the drop down list.


plz once look at the below pic..





can u plz post your controller code also ?



small mistake..


once observe the code in the comments at the end of the controller ...





just replace the line-35 with following statement -


return [select name,loc__c from Dept__c where name =: dept];

This was selected as the best answer

oh  sorry !


it's not line no 35, it is line 28.




got the output ..


Thank u so much Sir :)


If u dont mind , would u plz explain " name=:dept " ?


return [select name,loc__c from Dept__c where name =: dept];