0

I have below code where I need to show selected object's fields along with it's API Name, Label, Length, Type in a table format.

Can someone please let me know if I can do that using fieldpropertiesmap in below code?

    public void getFieldsPicklist(){
        toShowFields=true;

        fieldsOpt = new list<selectoption>();
        fieldsOpt.add(new selectoption('--NONE--','--NONE--'));
        jsondata = '[{';
        Map <String, schema.describefieldresult> fieldPropertiesMap;

        fieldMap = Schema.getGlobalDescribe().get(selectedObject).getDescribe().fields.getMap();
        fieldPropertiesMap = new Map <String, schema.describefieldresult> ();

        system.debug('fieldMap='+fieldMap.keySet());
        system.debug('fieldMap='+fieldMap.values());
        // List<Schema.SObjectField> fieldschema = new List<Schema.SObjectField>();
        List<Schema.SObjectField> fieldschema = new List<Schema.SObjectField>();
        for(Schema.SObjectField sfield : fieldMap.Values()) {
            schema.describefieldresult dfield = sfield.getDescribe();
            fieldsOpt.add(new selectoption(dfield.getname(),dfield.getname()));
            fieldPropertiesMap.put(dfield.getName(), dfield);
            fieldschema.add(sfield);
            system.debug('Name'+dfield.getName());
            system.debug('Label'+dfield.getLabel());
            system.debug('Length'+dfield.getLength());
            system.debug('Nill'+dfield.isNillable());
            system.debug('Type'+dfield.getType());
            system.debug('Unique'+dfield.isUnique());

        }
    } 
1
  • What issue are you having with this code? fieldPropertiesMap appears to simply map from field API name to the DescribeFieldResult. The latter object is what you need to obtain the field details you're looking for.
    – David Reed
    Jun 18, 2018 at 12:44

1 Answer 1

1

Apex Code

public class TestStackDev {
public String selectedOpt{get;set;}

//wrapper class
    class Data{
        public String name {get;set;}
        public String lable {get; set;}
        public String length {get;set;}
        public String Nill {get;set;}
        public String type {get;set;}
        public String unique {get;set;}     
    }
    public List<Data> getFieldsPicklist(){

        //return if there is no such object
        if(Schema.getGlobalDescribe().get(selectedOpt) == null){return null;}

        Map<String, schema.describefieldresult> fieldPropertiesMap = new Map <String, schema.describefieldresult> ();

        Map<String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get(selectedOpt).getDescribe().fields.getMap();


        List<Schema.SObjectField> fieldschema = new List<Schema.SObjectField>();

        List<Data> result = new List<Data>();
        for(Schema.SObjectField sfield : fieldMap.Values()) {

            schema.describefieldresult dfield = sfield.getDescribe();

            Data t = new Data();
            t.name  = dfield.getName();
            t.lable = dfield.getLabel();
            t.length = '' + dfield.getLength();
            t.Nill = dfield.isNillable()? 'TRUE': 'FALSE';
            t.type = '' + dfield.getType();
            t.unique = dfield.isUnique()? 'TRUE' : 'FALSE';
            result.add(t);

        }
        return result;
    }
}

Visual force Page

<apex:page  controller = "TestStackDev">

    <apex:pageBlock title="Hello {!$User.FirstName}!">
        <apex:form>
            <apex:actionFunction reRender="thePanels" name="yourFunction"/>
            <apex:selectList id="chooseColor" value="{!selectedOpt}" size="1" onchange="yourFunction()">

                <apex:selectOption itemValue="None" itemLabel="None"/>
                <apex:selectOption itemValue="User" itemLabel="Users"/>
                <apex:selectOption itemValue="Account" itemLabel="Accounts"/>

            </apex:selectList> 
        </apex:form>
    </apex:pageBlock>
    <apex:pageBlock title="Fields">

        <apex:pageBlockTable id="thePanels" value="{!FieldsPicklist}" var="item" rendered="true">

            <apex:column value="{!item.name}" headerValue="Name" />
            <apex:column value="{!item.lable}" headerValue="Lable" />
            <apex:column value="{!item.length}" headerValue="Length" />
            <apex:column value="{!item.Nill}" headerValue="Nill" />
            <apex:column value="{!item.unique}" headerValue="Unique" />

        </apex:pageBlockTable>
    </apex:pageBlock> 

</apex:page>

Screenshot

enter image description here

2
  • Thanks @pranav! This gets me compiled fine but getting this error when selecting the sobject. Return type of an Apex action method must be a PageReference. Found: core.apexpages.el.adapters.ApexListELAdapter Jun 18, 2018 at 13:23
  • @Testing_SFDC well its working for me on API version from 39 to 43. Make sure you've copied the code safely. Jun 18, 2018 at 14:25

You must log in to answer this question.

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