1

Looking to build a custom workbench which should list all the objects in the first drop down, depending on the selected object, the second drop down need to show respective fields.

Edit

Thanks sfdcfox, I am looking for some help in using describe sobject result based on the object selected in VF page , and a VF button such as "get required fields".

Schema.DescribeSObjectResult r = Account.sObjectType.getDescribe();

Map<String,Schema.SObjectField> M = r.fields.getMap();

for(String fieldName : M.keySet()) { 

          Schema.SObjectField field = M.get(fieldName);

          Schema.DescribeFieldResult F = field.getDescribe();

          //A nillable field can have empty content. A isNillable Boolean non-nillable field must have a value for the object to be                       //created or saved. 

         // if F.isNillable() is false then field is mandatory

         Boolean isFieldreq  = F.isNillable() ;

         System.debug ('F = ' + fieldName + ' isnul= ' +  isFieldreq);

}
1

This is a basic variation of something I've already written, but I've stripped it down to just the relevant parts for your question. Basically, we just need to use describe calls to get the relevant metadata, and display it in a list. This is not an optimized version, but meant as a demonstration.

public class ObjectFieldExplorerController {
    public String selectedObject { get; set; }
    public String selectedField { get; set; }
    public SelectOption[] getObjectOptions() {
        SelectOption[] results = new SelectOption[] { new SelectOption('','-- none --') };
        for(SObjectType sType: Schema.getGlobalDescribe().values()) {
            DescribeSObjectResult res = sType.getDescribe();
            results.add(new SelectOption(res.getName(), res.getLabel()));
        }
        return results;
    }
    public SelectOption[] getFieldOptions() {
        SelectOption[] results = new SelectOption[] { new SelectOption('','-- none --') };
        if(selectedObject != null) {
            for(SObjectField sField: Schema.getGlobalDescribe().get(selectedObject).getDescribe().fields.getMap().values()) {
                DescribeFieldResult res = sField.getDescribe();
                results.add(new SelectOption(res.getName(), res.getLabel()));
            }
        }
        return results;
    }
}

<apex:page controller="ObjectFieldExplorerController">
    <apex:form id="form">
        <apex:selectList value="{!selectedObject}" size="1">
            <apex:selectOptions value="{!objectOptions}" />
            <apex:actionSupport reRender="form" event="onchange" />
        </apex:selectList>
        <apex:selectList value="{!selectedField}" size="1">
            <apex:selectOptions value="{!fieldOptions}" />
        </apex:selectList>
    </apex:form>
</apex:page>

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.