0

I have a VF page to select Object and Picklistfield and then list out picklist values, if the pick list field is dependent picklist then the values need to be concatenated list of picklist value of controlling picklist and value of dependent picklist.

VF Page is

<!-- call custom controller -->
<apex:page controller="DVUController">
    <apex:form id="form">

        <apex:pageBlock title="Welcome {!$User.FirstName} to the Data Validation Utilities Page">

            <apex:pageBlockSection columns="5">

                <apex:pageblocksection title="Select Object" columns="1">
                    <apex:selectList value="{!selectedObject}" size="1">
                        <apex:selectOptions value="{!objectOptions}" />
                        <apex:actionSupport reRender="form" event="onchange" />
                    </apex:selectList>
                </apex:pageblocksection>

                <apex:pageblocksection title="Picklist Fields" columns="1">
                    <apex:pageblocktable value="{!picklistfields}" var="results">
                        <apex:column value="{!results}"></apex:column>
                    </apex:pageblocktable>
                </apex:pageblocksection>

                <apex:pageblocksection title="Select Picklist Field" columns="1">
                <apex:selectList value="{!selectedPicklistField}" size="1">
                        <apex:selectOptions value="{!pickListFieldOptions}" />
                    </apex:selectList>
                </apex:pageblocksection>

                <apex:pageblocksection title="Picklist Values" columns="1">
                <apex:pageblocktable title="Picklist Field Values" value="{!picklistvalues}" var="results">
                        <apex:column title="Picklist Field Values" value="{!results}"></apex:column>
                </apex:pageblocktable>
                </apex:pageblocksection>

            </apex:pageblocksection>
        </apex:pageBlock>
    </apex:form>
</apex:page>

The controller is

public class DVUController {

public String selectedObject { get; set; }
public String selectedField { get; set; }
public String selectedPicklistField { get; set; }
public List<String> picklistFields=new List<String>();
public List<String> picklistValues=new List<String>();


//generate list of Objects from the Org using Dynamic Apex

public SelectOption[] getObjectOptions() {
    SelectOption[] results = new SelectOption[] { new SelectOption('','-- none --') };
    for(SObjectType sobjectType: Schema.getGlobalDescribe().values()) {
        DescribeSObjectResult dsrao = sobjectType.getDescribe();
        results.add(new SelectOption(dsrao.getName(), dsrao.getLabel()));
        results.sort();
    }
    return results;    
}

 //generate list of names of PicklistFields and MultiPicklistFields from the Object

 public List<String> getPicklistFields(){
    if(selectedObject != NULL){
        //picklistfields = new List<String>();
        Schema.DescribeSObjectResult dsr = Schema.getGlobalDescribe().get(selectedObject).getDescribe();
        Map<String,Schema.SObjectField> fieldMap = dsr.fields.getMap();
        for(String fieldName : fieldMap.keySet()) { 
            Schema.SObjectField field = fieldMap.get(fieldName);
            Schema.DescribeFieldResult dfr = field.getDescribe();
               if( dfr.getType()==Schema.DisplayType.Picklist || dfr.getType()==Schema.DisplayType.MultiPicklist ) {
                  picklistfields.add(fieldName);
               }   
            system.debug('==>picklistfields==>'+picklistfields);
        }
        return picklistfields;          
    }
    return NULL;
}

 //generate list of PicklistFields from the Object as a dropdown

public SelectOption[] getPicklistFieldOptions() {
    SelectOption[] results = new SelectOption[] { new SelectOption('','-- none --') };
    if(selectedObject != null) {
        for(SObjectField sObjectField: Schema.getGlobalDescribe().get(selectedObject).getDescribe().fields.getMap().values()) {
            DescribeFieldResult dfr = sObjectField.getDescribe();
            if( dfr.getType()==Schema.DisplayType.Picklist || dfr.getType()==Schema.DisplayType.MultiPicklist ) {
            results.add(new SelectOption(dfr.getName(), dfr.getLabel()));
            results.sort();
        }
    }
    }
    return results;
} 

//generate list of Picklistvalues from the Picklist selected

 public List<String> getPicklistValues(){
         if(selectedPicklistField != NULL && selectedObject != NULL){
         //pickListValues = new List<String>();
         Schema.DescribeSObjectResult dsr = Schema.getGlobalDescribe().get(selectedObject).getDescribe();
         Map<String,Schema.SObjectField> fieldMap = dsr.fields.getMap();
            for(String fieldName : fieldMap.keySet()) { 
            Schema.SObjectField field = fieldMap.get(fieldName);
            Schema.DescribeFieldResult dfr = field.getDescribe();
            List<Schema.PickListEntry> picks = dfr.getPicklistValues();
            for(Schema.picklistentry v:picks){
            pickListValues.add(v.getValue());
             system.debug('==>pickListValues==>'+pickListValues);
     }
     return picklistValues;
 }

    }
    return picklistValues;
    }

  }

When I select the object and reset the picklist field doesnt reset, and when I select the picklist field for some reason Picklist values dont get displayed.Can some one please help

  • By minimizing the amount of code in your question, and providing a reproduce-able example you will increase the chance of your question being answered. Please take some time to review How to Ask and if you can modify your question to reflect this, that would be great! – glls May 7 '17 at 20:19
0

Please do the below corrections in so that your code could work. enter image description here

Make below changes in controller enter image description here

There is no native method for getting dependent picklist values. If that is very much required then you might want to explore http://titancronus.com/blog/2014/05/01/salesforce-acquiring-dependent-picklists-in-apex/

I hope this helps.

  • Thanks Kiran , is there anyway to display if the selected piccoloist field is restricted and/or required, i am unable to find any method in the describefieldresult class or picklistentry class. – user44735 May 8 '17 at 4:55
  • @user44735 Yes by describing field you can identify wether the field is restricted/dependent/required(isNillable) or not. developer.salesforce.com/docs/atlas.en-us.apexcode.meta/… If this helps please mark this as solution so that others could get help. – Kiran Machhewar May 8 '17 at 5:02
  • Thank you.How do I rerender the whole page , for example I select the Object and then select the picklist field - which will display picklist values. However now when I select a different object the picklist drop down doesnt reflect the object, it stays static.Thank you – user44735 May 8 '17 at 16:57
  • @user44735 Whenever you want reRender any section there are two things that you need to do. #1. Have event (ActionFunction, Action Support) configured for the the element based on which you want to render section. #2. Assign the id to the section that you would like to reRender and mention it as a part of reRender attribute in configured action as part of #1. – Kiran Machhewar May 8 '17 at 21:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.