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
vcvvcv 

Populate multiple picklists with records from three different custom objects

I have three related objects and I wanto to populat picklists with records from different fields.


Example: Object1__c.Name__c (text field) Object2__c.AvailableAppointments__c (date), Object3__c.AppointmentDuration (minutes).


The following code works fine, however it only displays the picklist for Object1__c.Name__c. How can I add two additional picklists with values from Object2 and Object3 fields?

VF:

<apex:page controller="sampleCon">
    <apex:form >
        <apex:selectList value="{!strSelected}" multiselect="false" size="1">
            <apex:selectOptions value="{!options}"/>
        </apex:selectList>
    </apex:form>
</apex:page>
Controller:
public class sampleCon {

 public String strSelected  {get;set;}
 public List<SelectOption> options {get;set;}

 public sampleCon() {
    strSelected = '';
    options = new List<SelectOption>();

    for(Object1__c obj :[SELECT Name FROM Object1__c]) 
    {
        options.add(new SelectOption(obj.Id,obj.Name));
    }

 }
}

 
Best Answer chosen by vcv
Raj VakatiRaj Vakati
Modify your controller as shown below 
public class sampleCon {

 public String strSelected  {get;set;}
 public List<SelectOption> options {get;set;}

 public sampleCon() {
    strSelected = '';
    options = new List<SelectOption>();

    for(Object1__c obj :[SELECT Name FROM Object1__c]) 
    {
        options.add(new SelectOption(obj.Id,obj.Name));
    }
	for(Object2__c obj :[SELECT Name FROM Object2__c]) 
    {
        options.add(new SelectOption(obj.Id,obj.Name));
    }
	for(Object3__c obj :[SELECT Name FROM Object3__c]) 
    {
        options.add(new SelectOption(obj.Id,obj.Name));
    }

 }
}

 

All Answers

Raj VakatiRaj Vakati
Modify your controller as shown below 
public class sampleCon {

 public String strSelected  {get;set;}
 public List<SelectOption> options {get;set;}

 public sampleCon() {
    strSelected = '';
    options = new List<SelectOption>();

    for(Object1__c obj :[SELECT Name FROM Object1__c]) 
    {
        options.add(new SelectOption(obj.Id,obj.Name));
    }
	for(Object2__c obj :[SELECT Name FROM Object2__c]) 
    {
        options.add(new SelectOption(obj.Id,obj.Name));
    }
	for(Object3__c obj :[SELECT Name FROM Object3__c]) 
    {
        options.add(new SelectOption(obj.Id,obj.Name));
    }

 }
}

 
This was selected as the best answer
vcvvcv
Perfect, thank you.