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
Nidhi Sharma 17Nidhi Sharma 17 

How to fetch various record types as picklist values for a field

In Applications (custom object), I have to create a custom picklist whose values should be various record types created for Applications. And if any record type is deleted, it gets reflected in the picklist values.
Please help.
Best Answer chosen by Nidhi Sharma 17
Himanshu ParasharHimanshu Parashar
 Hi Nidhi,

You can query recordtype object and create custom picklist as shown below

Apex Class method
public List<selectOption> getRecordTypes() {
        List<selectOption> options = new List<selectOption>(); 
//new list for holding all of the picklist options
        options.add(new selectOption('', '- None -')); 
//add the first option of '- None -' in case the user doesn't
 want to select a value or in case no values are
 returned from query below
        for(Recordtype record : [SELECT Id,Name,SobjectType FROM RecordType WHERE SobjectType = 'Opportunity') { 
//query for Record types records 
            options.add(new selectOption(
record.id, record.Name)); 
//for all records found - add them to the picklist options
        }
        return options; //return the picklist options
}

public String selectedrecordtype{get;set;}

VF Page:
 
<apex:selectList id="recordtypes" value="{!selectedrecordtype}" size="1" title="Recordtypes">
<apex:selectOptions value="{!RecordTypes}"></apex:selectOptions>
</apex:selectList>



Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S.  If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
 

All Answers

Himanshu ParasharHimanshu Parashar
 Hi Nidhi,

You can query recordtype object and create custom picklist as shown below

Apex Class method
public List<selectOption> getRecordTypes() {
        List<selectOption> options = new List<selectOption>(); 
//new list for holding all of the picklist options
        options.add(new selectOption('', '- None -')); 
//add the first option of '- None -' in case the user doesn't
 want to select a value or in case no values are
 returned from query below
        for(Recordtype record : [SELECT Id,Name,SobjectType FROM RecordType WHERE SobjectType = 'Opportunity') { 
//query for Record types records 
            options.add(new selectOption(
record.id, record.Name)); 
//for all records found - add them to the picklist options
        }
        return options; //return the picklist options
}

public String selectedrecordtype{get;set;}

VF Page:
 
<apex:selectList id="recordtypes" value="{!selectedrecordtype}" size="1" title="Recordtypes">
<apex:selectOptions value="{!RecordTypes}"></apex:selectOptions>
</apex:selectList>



Thanks,
Himanshu
Salesforce Certified Developer | Administrator | Service Cloud Consultant

P.S.  If my answer helps you to solve your problem please mark it as best answer. It will help other to find best answer.
 
This was selected as the best answer
Nidhi Sharma 17Nidhi Sharma 17
Thanks a lot Himanshu. That worked!
Himanshu ParasharHimanshu Parashar
Your welcome!.

Thanks,
Himanshu