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
affableaffable 

get employee records based on deptartment number selection

i have created emp2__c object for employees and dep2__c object  for department. ,  emp2__c is related to dep2__c (lookup). if i select deptName and press btton go i should view the records of corresponding employees associated with department name . but  instead am getting ids. help me in this issue.

 

controller:

public class departmentConrtroller
{
List<dept2__c> deptList;// departmentList
List<emp2__c> employeesList;//employeeList

String departmentNumber='10';
public departmentConrtroller()
{
 deptList =[select Id,name,dept2No__c,Location__c from dept2__c];
 }
//select options
public List<SelectOption> getdepartmentItems()
{

List<selectOption> options = new List<selectOption>();
 
for(dept2__c dept : deptList)
 {
  String departmentNumber1 =String.valueOf(dept.dept2No__c);
  options.add(new selectOption(departmentNumber1,dept.name));
 }
 return options;
}
 
 public String getdepartmentNumber()
 {
 return departmentNumber;
 }
 public void setdepartmentNumber(String departmentNumber)
 {
 this.departmentNumber=departmentNumber;
 }
 public List<emp2__c> getemployees()
 {
  Id id=[select Id from dept2__c where dept2No__c=:departmentNumber].ID;
  employeesList=[select name,employeeId__c,  salary__c,Address__c from emp2__c where dept2No__c=:id];
  return employeesList;
 }
 public PageReference go()
 {
 return null;
 }
 }

 

visualpage: sx

 

 

<apex:page controller="departmentConrtroller">
<apex:form >
<apex:selectList value="{!departmentNumber}">
<apex:selectOptions value="{!departmentItems}"/>
</apex:selectList>
<apex:commandButton value="go" action="{!go}"/ >

</apex:form>
<apex:outputText value="{!employees}"/>
</apex:page>

Best Answer chosen by Admin (Salesforce Developers) 
bob_buzzardbob_buzzard

I think this is because you are simply rendering the employee list via an outputText component - this will generate the string representation of the employee list, which could well be just the ids.

 

It would be better to iterate the list in some way - via a datatable for example:

 

<apex:dataTable value="{!employees}" var="emp">
   <apex:column value="{!emp.name} />
   <apex:column value="{!emp.employeeId__c}" />
   <apex:column value="{!emp.salary__c}" />
   <apex:column value="{!emp.Address__c}" />
</apex:dataTable>

 

All Answers

bob_buzzardbob_buzzard

I think this is because you are simply rendering the employee list via an outputText component - this will generate the string representation of the employee list, which could well be just the ids.

 

It would be better to iterate the list in some way - via a datatable for example:

 

<apex:dataTable value="{!employees}" var="emp">
   <apex:column value="{!emp.name} />
   <apex:column value="{!emp.employeeId__c}" />
   <apex:column value="{!emp.salary__c}" />
   <apex:column value="{!emp.Address__c}" />
</apex:dataTable>

 

This was selected as the best answer
affableaffable

thanks mate it worked!!