0

UPDATE: instead of me checking like this: "{!SelectedRecordType== '012000000000000XAA'}

<apex:pageBlockSection rendered="{!SelectedRecordType== '012000000000000XAA'}">

I want to do:

"{!SelectedRecordType== 'Master'}

<apex:pageBlockSection rendered="{!SelectedRecordType== 'Master'}">

I have a dropdownlist based on the user selection I'm showing pageBlockSection below is the code the code works fine except one issue, the issue is that instead of getting and Id I want to compare with Label as you can see the image if I select for an example any item from the dropdownlist I'm getting Id but my questions is how can I get name for comparing what user have selected?

<apex:selectList id="rt2" label="Record Types 3" value="{!SelectedRecordType}">
    <apex:selectOption itemValue="" itemLabel=""/> 
      <apex:actionSupport event="onchange" reRender="pb" /> 
       <apex:selectOptions value="{!RecordTypes}"/>
 </apex:selectList> 


<apex:outputpanel layout="none" id="pb">
   <apex:pageBlockSection rendered="{!SelectedRecordType== 'Job'}" columns="1">
   <apex:outputText >job section</apex:outputText>
   </apex:pageBlockSection>

Apex:

public string selectedRecordType {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)
    {
      rTypes.add(new SelectOption(info.getRecordTypeId(), info.getName()));
    } 
    return rTypes;
}       

enter image description here

2
  • the other option is to use action method call to the controller but i'm curious if that can be achieve without using any action method
    – Nick
    Feb 24, 2016 at 2:59
  • 1
    I tend to prefer controller methods isXXXX with getter or getIsXXX() so my VF page is less tied to specific fields and I can use regression testmethods to verify conditions.
    – cropredy
    Feb 24, 2016 at 6:20

1 Answer 1

1

One option would be to keep a map from the Id to the text value in the page controller and get the value from there. Here is an example that I got to work in my system:

Apex:

public  class MyController {

    public MyController() {
            RecordTypeIdToName = new map<Id,String>();
    }

    public string selectedRecordType {get;set;}
    public string selectedRecordTypeLabel {
        get{
            system.debug(selectedRecordType);
            system.debug(RecordTypeIdToName);
            return getRecordTypeName(selectedRecordType);
        }
    }
    private map<Id,String> RecordTypeIdToName {get; set;}      
    public List<selectOption> getRecordTypes()
       {
        List<selectOption> rTypes = new List<selectOption>();  
        String objectAPIName = 'Account' ; //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)
        {
          rTypes.add(new SelectOption(info.getRecordTypeId(), info.getName()));
          RecordTypeIdToName.put(info.getRecordTypeId(), info.getName());      
        } 
        return rTypes;
    }

    private String getRecordTypeName(Id RecordTypeId){   
        return RecordTypeIdToName.get(RecordTypeId);
    }

}

Visualforce:

<apex:page controller="MyController">
<apex:form >

<apex:selectList id="rt2" label="Record Types 3" value="{!SelectedRecordType}" multiselect="false" size="1">
    <apex:actionSupport event="onchange" reRender="pb" /> 
    <apex:selectOptions value="{!RecordTypes}"/>
</apex:selectList>

<apex:outputpanel layout="none" id="pb">
    <apex:pageBlock >
       <apex:outputText >{!selectedRecordTypeLabel}</apex:outputText>
    </apex:pageBlock>
</apex:outputPanel>

</apex:form>
</apex:page>
12
  • how does the vfp code looks like, is that the only option?
    – Nick
    Feb 24, 2016 at 3:01
  • i have posted in my question, i guess what i'm asking is that what changes do i need to do in visualforce page
    – Nick
    Feb 24, 2016 at 3:04
  • Where is the Id value coming from then on the page?
    – martin
    Feb 24, 2016 at 3:07
  • And also, what are you using the Id for?
    – martin
    Feb 24, 2016 at 3:08
  • i'm using <apex:actionSupport event="onchange" in my page
    – Nick
    Feb 24, 2016 at 3:08

You must log in to answer this question.

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