+ Start a Discussion
pooja biswaspooja biswas 

bind recordtype to picklist

Hi
My requirement is to bind recordtypes to an picklist and display in visual force page
I have the code show in this post.
public with sharing class recordtypepicklist
{
   public list<SelectOption> getRecordTypes()
   {
    list<SelectOption> options = new list<SelectOption>();
    
    for(RecordType sRecordType:[select ID,Name from RecordType where SObjectType='Contact'])
    {
     options.add(new SelectOption(sRecordType.ID,sRecordType.Name));
    }
     return options;
  }  
}
I am getting an error as Error: Compile Error: Variable does not exist: ID at line 9 column 38
Its coming from the statement inside the for loop.

Pls let me know how to resolve this.

pooja

 
Best Answer chosen by pooja biswas
CloudGeekCloudGeek
Hello Pooja,

Try something like :
 
public class MyController {

list<SelectOption> options {get;set;}
public String selectedCountry{get;set;}
public String selectedRecordType{get;set;}

public MyController(ApexPages.StandardController controller) {    }

public list<SelectOption> getRecordTypes()
   {
        options = new list<SelectOption>();
    
    for(RecordType rt: [SELECT Name FROM RecordType WHERE SobjectType = 'Account'])
    {
         if(rt != null)
             options.add(new SelectOption(rt.Id,rt.Name));
    }
    
    System.debug('@@@ types List = '+options);
     return options;
  }  
  
}

<apex:page controller="MyController">
 
  <apex:form >
  <apex:pageblock >
              
    <apex:outputLabel > Record Types : </apex:outputLabel>
    <apex:selectList size="1" value="{!selectedRecordType}">
      <apex:selectOptions value="{!RecordTypes }"/>
    </apex:selectList> <br/>
    
  </apex:pageblock>
  
  </apex:form>
</apex:page>


You will get to see :

User-added image


Note:  Mark this as solution if that resolves your issue.

All Answers

KevinPKevinP
Pooja,

Are you sure that your inline query is returning rows ? I would break it out a little differently, something like this:
 
list<SelectOption> options = new list<SelectOption>();
    list<recordType> recTypes = new List<recordType>();
    try {
       recTypes = [select ID,Name from RecordType where SObjectType='Contact'];
    } catch (QueryException qe) {
        system.debug('cant find any record types');
    }

    for(RecordType sRecordType : recTypes) {
     options.add(new SelectOption(sRecordType.ID,sRecordType.Name));
    }

The key here is, to catch that query exception (it'll say something like list has no rows for assignment); before trying to add to the options.
pooja biswaspooja biswas
Hi kevin
Thanks for ur reply
I am getting an error as " Illegal assignment from List<RecordType> to List<recordtype> at line 15 column 10"
Its coming from the statement inside the try block.
This is strange because in workbench we have RecordType object.
public with sharing class recordtypepicklist
{
    public String selectedrecordtype{get;set;}
    
    List<selectOption> options = new List<selectOption>();
      
    public List<selectOption> getRecordTypes() 
    {
       List<RecordType> recTypes = new List<RecordType>();
       
       try 
       { 
         recTypes=[select ID,Name from RecordType where SObjectType='Contact']; 
       }
       
       catch(QueryException qe) 
       { 
        system.debug('cant find any record types for contact'); 
       } 

       if (recTypes.size() > 0)
       {
          options.add(new selectOption('', '- None -'));
           
          for(Recordtype record : recTypes)
          {
           options.add(new selectOption(record.id, record.Name));
          }
       }
       return options; //return the picklist options
   }
}
Could u pls let me know the issue?

thanks
pooja biswas
 
KevinPKevinP
Do you have a custom object RecordType? Otherwise, check spelling?
pooja biswaspooja biswas
Hi kevin
I do not have any custom object RecordType.


 
CloudGeekCloudGeek
Hello Pooja,

Try something like :
 
public class MyController {

list<SelectOption> options {get;set;}
public String selectedCountry{get;set;}
public String selectedRecordType{get;set;}

public MyController(ApexPages.StandardController controller) {    }

public list<SelectOption> getRecordTypes()
   {
        options = new list<SelectOption>();
    
    for(RecordType rt: [SELECT Name FROM RecordType WHERE SobjectType = 'Account'])
    {
         if(rt != null)
             options.add(new SelectOption(rt.Id,rt.Name));
    }
    
    System.debug('@@@ types List = '+options);
     return options;
  }  
  
}

<apex:page controller="MyController">
 
  <apex:form >
  <apex:pageblock >
              
    <apex:outputLabel > Record Types : </apex:outputLabel>
    <apex:selectList size="1" value="{!selectedRecordType}">
      <apex:selectOptions value="{!RecordTypes }"/>
    </apex:selectList> <br/>
    
  </apex:pageblock>
  
  </apex:form>
</apex:page>


You will get to see :

User-added image


Note:  Mark this as solution if that resolves your issue.
This was selected as the best answer
pooja biswaspooja biswas
Hi cloudGeek
I moved the entire code to another user account and it works.
But the picklist remains empty in visual force page.

Thanks
pooja
 
CloudGeekCloudGeek
Hi Pooja,

Did you try to run this QUERY in Developer Console and see if you are getting any rows ?
 
recTypes=[select ID,Name from RecordType where SObjectType='Contact'];

If you don't get any rows then your picklist will be empty.

 
CloudGeekCloudGeek
Hello Pooja,

Replace your controller like below :
 
public class MyController {

list<SelectOption> options {get;set;}
public String selectedRecordType{get;set;}

public MyController(ApexPages.StandardController controller) {   }

public list<SelectOption> getRecordTypes()
   {
        options = new list<SelectOption>();

        SObjectType ObjType =  Account.SObjectType;
    
        Map<String,Schema.RecordTypeInfo> recordTypeInfo = ObjType.getDescribe().getRecordTypeInfosByName();

        options = new list<SelectOption>();

    for(String RecTypeName : recordTypeInfo.keySet())
    {   
      options.add(new SelectOption(recordTypeInfo.get(RecTypeName).getRecordTypeId(),RecTypeName));
     
    }
        System.debug('@@@ types List = '+options);
        
     return options;
  }  
  
  
}

}

Let me know if that helps!