+ Start a Discussion
VKrishVKrish 

Using Record Type in Apex

Intro about my situation...

I have defined 2 custom record type (RT1, RT2) in a custom object (say Custom__c). Each Record type has its own set of customized Picklist values (Custom_Type__c) & available to different set of Profiles (say R1 is available & default to Profiles X,Y,Z and R2 is available & default for Profiles A,B,C).

In a custom VF Page, I have one of the picklist value & depending up on the values they select, I will be displaying list of records to edit.

 

I need to develop...

Depending up on the user (from Profile A) who access the page, I want to display only the set of picklist values (Custom_Type__c values defiled under RT1).

I want to hide those picklist values that are not available ie., not defined for their respective Record Set.

Also I want make the default picklist value default in the page which defined in the Record Set's custom picklist field.

 

Initially I thought from Winter 11 release, this is made available automatically as in standard SF page. But its not fetching any data related to Record Set. It is displaying only the values defined in Master Record set.

 

I tried to use Custom__c.SObjectType.getDescribe().getRecordTypeInfosByName() &

Custom__c.Custom_Type__c.getDescribe().getPicklistValues()

But this returns only the master Record Type Picklist values & I couldn't continue much.

 

It would be great if anyone could guide me through proper methods to fetch the result I want.

Any help is much appreciated

 

Thanks in advance

Best Answer chosen by Admin (Salesforce Developers) 
VKrishVKrish

Thanks a lot! That blog gives me some good idea.

But I solved this issue in a simple step.

 

Initially I used apex:inputField in VF page & used a apex class to get dynamic picklist values. The Describe API was not filtering properly.

But when I took off the apex class & fetch the values directly, it automatically filters with the appropriate Record Type.

// Use this
<apex:inputField id="filtergroups" styleClass="filtergroups" value="{!Custom__c.Custom_Field__c}"/>

// Do not use apex method to fetch data
<apex:selectList id="filtergroups" styleClass="filtergroups" size="1">
      <apex:selectOptions value="{!FndTypeOptions}"/>
</apex:selectList>

 

So all we have to do is NOT to use Apex classes & methods to fetch pick list values.

All Answers

VKrishVKrish

I noticed that this bug has been fixed & its automatically available via apex code from Winter 11.

But the following code still gives me only the master list of pick list values and not pick list value defined under the Record type available for that particular user profile.

Schema.getGlobalDescribe().get('SObject').getDescribe().fields.getMap().get('Custom_Field__c').getDescribe().getPicklistValues()

Am I missing something? Am I the only one still facing this problem?

Please help.

Salesforce WizardSalesforce Wizard

Vkrish,

 

I can't find anything with dynamic apex that supports returning the picklist values available within a single record type. All the RecordType options I've seen get you data about the Record type itself -- not the actual fields with that record type.

 

I did find this blog: http://bobbuzzard.blogspot.com/2012/01/record-type-picklist-values.html

 

That has a solution. Basically he's using the visualforce page to send the values of the picklist to his controller and then spitting those values out. It's a round-about-way of doing things, but it may get your immediate issue solved.

VKrishVKrish

Thanks a lot! That blog gives me some good idea.

But I solved this issue in a simple step.

 

Initially I used apex:inputField in VF page & used a apex class to get dynamic picklist values. The Describe API was not filtering properly.

But when I took off the apex class & fetch the values directly, it automatically filters with the appropriate Record Type.

// Use this
<apex:inputField id="filtergroups" styleClass="filtergroups" value="{!Custom__c.Custom_Field__c}"/>

// Do not use apex method to fetch data
<apex:selectList id="filtergroups" styleClass="filtergroups" size="1">
      <apex:selectOptions value="{!FndTypeOptions}"/>
</apex:selectList>

 

So all we have to do is NOT to use Apex classes & methods to fetch pick list values.

This was selected as the best answer