0

I have got this code snippet here:

list<SelectOption> options = new list<SelectOption>();
  Schema.DescribeSObjectResult objDescribe = Account.sObjectType.getDescribe();
    map<String, Schema.SObjectField> fieldMap = objDescribe.fields.getMap(); // Get a map of fields for the SObject 
    list<Schema.PicklistEntry> values = fieldMap.get('Industry').getDescribe().getPickListValues(); // Get the list of picklist values for this field.

The problem is that I want to have the Industry picklist values according to a particular record type values at Account level. Is there any way I can filter the list here based on record type and show it on the Visual force page? Using Apex only.

I thought that I can use describeLayout() to resolve this? Any help will be much appreciated.

3 Answers 3

1

If I (correctly) understand what you're trying to do, then I'd say you're going about things the hard way.

My understanding is that your Industry picklist is a dependent picklist where the values are controlled by recordtype.

In situations like these, I like to use what I call a dummy object in a controller or extension.

That would look like this:

public class MyControllerExtension{
    // The dummy variable is just here to make life easier for us.
    // It needs to be publicly readable to be used in visualforce.
    // Generally, dummy variables themselves aren't saved along with other data,
    //   though if you override or create your own save() method, you can use the
    //   dummy variable to set values on the object instance that you are saving.
    public Account dummyAccount {get; set;}
    public MyControllerExtension(ApexPages.StandardController stdCon){
        dummyAccount = new Account(
            // RecordTypeInfosByName is a map keyed on the record type's label, rather than
            //   the API name (which in my example would be My_Record_Type_Label).
            // Basically, leave any spaces alone instead of converting them to 
            //   underscores
            RecordTypeId = Schema.SObjectType.Account.getRecordTypeInfosByName().get('My Record Type Label').getRecordTypeId()

            // If you get a 'field referenced without being queried' error, then
            //   the simple fix would be to just include that field here in the
            //   constructor for the relevant dummy object.
            // Don't forget to add a comma after the assignment to recordTypeId
            //   if you uncomment this line!
            //Industry = null
        );
    }
}

With that in place, you can add the picklist to your visualforce page quite simply

<apex:form>
    <!-- Other input/output fields that you already have would go here -->
    <!-- Using apex:inputField here causes Salesforce to do the grunt work of
           figuring out exactly which picklist values to display. -->
    <apex:inputField value="{!dummyAccount.Industry}" />
</apex:form>
0

In Apex you can't filter picklist values by recordtypes. Nonetheles, I believe Visualforce does respect the visibility of them, so you shouldn't have any problems.

Regarding the Apex part, here is an Idea: https://success.salesforce.com/ideaView?id=08730000000gNpLAAU

0

For those that are curious about doing this in Apex, one of the projects I was working on required this. This code snippet seems to do the trick if the running user has the API permission enabled:

@AuraEnabled
public static void calloutForRtValues(String sObjectName, String recordTypeId, String fieldName) {
    String sfdcUrl = Url.getOrgDomainUrl().toExternalForm();
    String endpoint = '/services/data/{!CURRENT_API_VERSION (e.g. v42.0)}/ui-api/object-info/' + sObjectName + '/picklist-values/' + recordTypeId + '/' + fieldName;
    HttpRequest request = new HttpRequest();
    request.setMethod('GET');
    request.setHeader('Authorization', 'OAuth ' + UserInfo.getSessionId());
    request.setEndpoint(sfdcUrl + endpoint);
    HttpResponse response = new Http().send(request);
    System.debug('responseBody: ' + response.getBody());
    //Parsing logic....
}

You must log in to answer this question.

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