-1

How to load the RecordTypes name in the dropdown and below is the code I'm using and its pulling the right recordtype names but I could not able to figure out how to display in the Visualforce page:

Apex code:

public List<selectOption> getRecordTypes()
{
    List<selectOption> rTypes = new List<selectOption>();  
    String objectAPIName = 'Case' ; //any object api
      Schema.DescribeSObjectResult sobjectResult = Schema.getGlobalDescribe().get(objectAPIName).getDescribe();
       List<Schema.RecordTypeInfo> recordTypeInfo = sobjectResult.getRecordTypeInfos();
        Map<String,Id> mapofCaseRecordTypeNameandId = new Map<String,Id>();
        for(Schema.RecordTypeInfo info : recordTypeInfo){
         mapofCaseRecordTypeNameandId.put(info.getName(),info.getRecordTypeId());
        }
         system.debug('***mapofCaseRecordTypeNameandId*'+mapofCaseRecordTypeNameandId);
  return rTypes;
}

Visualforce page:

<apex:pageBlockSection title="Select Record Type" columns="1"> 
   <apex:selectList id="countries" value="{!RecordType.Name}" size="1" required="true">
    <apex:selectOptions value="{!RecordTypes.Name}"/>
     </apex:selectList>    
    </apex:pageBlockSection>

3 Answers 3

3
  1. You need to have additional property for your apex:selectList tag to refer the selected value.

  2. As @Namfuak said, you need to add SelectOption values to populate them on the page.

so the modified code looks like this: (Assuming this is inside a selectControllerClass)

public class selectController
{
   // This will be a list<String> if your SelectList is multiSelect type
   public string selectedRecordTypeId {get;set;}

   public List<selectOption> getRecordTypes()
   {
    List<selectOption> rTypes = new List<selectOption>();  
    String objectAPIName = 'Case' ; //any object api
    Schema.DescribeSObjectResult sobjectResult = Schema.getGlobalDescribe().get(objectAPIName).getDescribe();
    List<Schema.RecordTypeInfo> recordTypeInfo = sobjectResult.getRecordTypeInfos();
    Map<String,Id> mapofCaseRecordTypeNameandId = new Map<String,Id>();
    for(Schema.RecordTypeInfo info : recordTypeInfo)
    {
       mapofCaseRecordTypeNameandId.put(info.getName(),info.getRecordTypeId());

      //This line renders the SelectOptions in your list. 
      rTypes.add(new SelectOption(info.getRecordTypeId(), info.getName()));
    }
    system.debug('***mapofCaseRecordTypeNameandId*'+mapofCaseRecordTypeNameandId);
    return rTypes;
}

Page

 <apex:pageBlockSection title="Select Record Type" columns="1"> 
 <apex:selectList id="countries" value="{!selectedRecordTypeId}" size="1" required="true">
 <apex:selectOptions value="{!RecordTypes}"/>
 </apex:selectList>    
</apex:pageBlockSection>
3

To add to @sam_s answer, the controller can somewhat more concise and avoid the global describe call. The map should be unnecessary as presumably you'll be assigning the selected RecordTypeId to an sobject prior to saving it.

public class selectController {
   // This will be a list<String> if your SelectList is multiSelect type
   public string selectedRecordTypeId {get;set;} // also use to default to a recordTypeId

   public List<selectOption> getRecordTypes() {
    List<selectOption> rTypes = new List<selectOption>();  
    for(Schema.RecordTypeInfo rti : Case.SobjectType.getDescribe().getRecordTypeInfos()) { 
      //This line renders the SelectOptions in your list. 
      rTypes.add(new SelectOption(rti.getRecordTypeId(), rti.getName()));
    }

    return rTypes;
}

Page

 <apex:pageBlockSection title="Select Record Type" columns="1"> 
 <apex:selectList id="countries" value="{!selectedRecordTypeId}" size="1" required="true">
 <apex:selectOptions value="{!RecordTypes}"/>
 </apex:selectList>    
</apex:pageBlockSection>
1

At some point in your loop, you have to add new SelectOptions to your list with rTypes.add(new SelectOption(info.getName(), info.getName()) (or whatever you want to use for value and label), the change <apex:selectOptions value="{!RecordTypes.Name}"/> to <apex:selectOptions value="{!RecordTypes}"/>. Your list of SelectOptions has no property called Name.

2
  • i have tried before posting it but same result
    – Nick
    Commented Feb 23, 2016 at 21:25
  • @AbuHamzah You also never added the record types to your list, I've added a bit about that. Commented Feb 23, 2016 at 21:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .