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
ravi1234.ax1565ravi1234.ax1565 

picklist in visualforce

 hello group,

            

                      i hav two s-obj enquiry and student. in enquiry i want a picklist called course. iam trying to retrieve course name  from course obj in enquiry (picklist). but i dont have any picklist in  enquiry related to course name picklist in datamodel. and also there is no relationship between two s-obj. is this possilbe to retrieve a field from one s-obj to another one in to picklist with out any relationship....

 

               
pls guide in a r8 way

 

     thanks for giving reply(in advance)          

ibtesamibtesam

In the Controller :

 

Query the course from database

 

public String selectedCourse{get; set;}

List<SelectOption> courseList = new List<SelectOption>();

and iterate and add those values in a select option List

 

For(String courseName :  [select id,coursename from course where 'Your condition' ]){

courseList.add(new Selectoption(coursename,coursename) );

}

 

In the VF Page :

 

<apex:selectList size="1"  value="{!selectedCourse}" id="courselist" >
  <apex:selectOptions value="{!courseList}" ></apex:selectOptions>
   </apex:selectList>

 

This becomes a picklist without having to have relation between two objects.

ViwalViwal

Hi Ravi,

 

Yes it's possible.

 

Try below code without soql query.

 

first of all query the field in the Schema

Schema.DescribeFieldResult fieldResult = Student__c.courses__c.getDescribe();

 

Save this in a List

List<Schema.PicklistEntry> ple = fieldResult.getPicklistValues();

 

Iterate over list and get the label value pair

List<SelectOption> options = new List<SelectOption>();
for( Schema.PicklistEntry f : ple)
{
options.add(new SelectOption(f.getLabel(), f.getValue()));
}

 

In VF

 

<apex:selectList value="{!enquiry.SelectedCourses}" size="1" >
<apex:selectOptions value="{!options}"/>
</apex:selectList>

 

 

If this answers your query please mark is as Solution.