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
Harsha ShriHarsha Shri 

Populate list values into apex:select

Hi All,
I am getting record types by using below list

List<RecordType> recList= new List<RecordType>([select id, name from recordtype where sobjecttype='obj1__c']);

I want to show this values in apex:select. How can I do this
I tried some ways, but not working.
Please help me

Thanks in Advance!!
Best Answer chosen by Harsha Shri
v varaprasadv varaprasad
Hi Harsha,

Please check  once below sample code : 
 
<apex:page controller="Recordtypes" id="pg">
    <apex:form >
        <apex:pageBlock title="Recordtypes" id="pd">
            <apex:selectList size="1" value="{!selrecordtype}" >
                <apex:actionSupport event="onchange" reRender="panel1,panel2" />
                RecordTypes :  <apex:selectOptions value="{!lstOfRectypes}">
                
                </apex:selectOptions>
            </apex:selectList>
            <apex:outputPanel id="panel1">   
                  <apex:pageBlockSection rendered="{!selrecordtype=='International'}" >
                     Field1 :  <apex:inputText />
                  </apex:pageBlockSection>
              </apex:outputPanel> 
            <apex:outputPanel id="panel2">   
                  <apex:pageBlockSection rendered="{!selrecordtype=='US Lead'}" >
                     Field2 :  <apex:inputText />
                  </apex:pageBlockSection>
              </apex:outputPanel> 
        </apex:pageBlock>
        
    </apex:form>
</apex:page>

Hope this helps you.
If it helps you please mark it as the best answer.


Thanks
Varaprasad 


 

All Answers

v varaprasadv varaprasad
Hi Harsha,

Please find sample code in below Link : 

https://developer.salesforce.com/forums/ForumsMain?id=9060G0000005P0vQAE


Hope this helps.
If it helps you please mark it as the best answer.


Thanks
Varaprasad

 
Jonathan Thornton 4Jonathan Thornton 4
Hello,

You could do something like this:

//In your constructor:

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

//When you pull your query add them to the select option list:

List<RecordType> recList= new List<RecordType>([select id, name from recordtype where sobjecttype='obj1__c']);

for(RecordType rt : recList) {
    options.add(new SelectOption(rt.Name));


//Then in your visualforce page simply reference options:

<apex:selectList value="{!options}" id="options"/>

//This is quite simplified but that should do the trick.

Good luck!
Jonathan
 
Harsha ShriHarsha Shri

Hi Varaprasad,
Thank you very much for much for your help. Its working great

now when I select record type, I want show some fileds in output panel in same visualforce page. like rendering section based on record type. 
how can I do this

v varaprasadv varaprasad
Hi Harsha,

Please check  once below sample code : 
 
<apex:page controller="Recordtypes" id="pg">
    <apex:form >
        <apex:pageBlock title="Recordtypes" id="pd">
            <apex:selectList size="1" value="{!selrecordtype}" >
                <apex:actionSupport event="onchange" reRender="panel1,panel2" />
                RecordTypes :  <apex:selectOptions value="{!lstOfRectypes}">
                
                </apex:selectOptions>
            </apex:selectList>
            <apex:outputPanel id="panel1">   
                  <apex:pageBlockSection rendered="{!selrecordtype=='International'}" >
                     Field1 :  <apex:inputText />
                  </apex:pageBlockSection>
              </apex:outputPanel> 
            <apex:outputPanel id="panel2">   
                  <apex:pageBlockSection rendered="{!selrecordtype=='US Lead'}" >
                     Field2 :  <apex:inputText />
                  </apex:pageBlockSection>
              </apex:outputPanel> 
        </apex:pageBlock>
        
    </apex:form>
</apex:page>

Hope this helps you.
If it helps you please mark it as the best answer.


Thanks
Varaprasad 


 
This was selected as the best answer
Harsha ShriHarsha Shri

Hi Varaprasad,

Thank you very much for helping me 

This is the controller class code you have given to me
 

public class Recordtypes {
    public list<selectoption> lstOfRectypes{set;get;}
    List<RecordType> recList;
    
    public Recordtypes(){
        lstOfRectypes =  new list<selectoption>();
        lstOfRectypes.add(new selectoption('None','None'));
        recList= new List<RecordType>([select id, name from recordtype where sobjecttype='Lead']);
        for(RecordType rd : recList){           
            lstOfRectypes.add(new selectoption(rd.name,rd.name));
        }
    }
}

I am not able to find out the method "selrecordtype" which you are refering in visualforce page.
Can you please help with this