function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
keshin okawakeshin okawa 

Q>Need help with code about retrieving field names

I was able to utilize some code I found online and finally come up with this one..

Class
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
public with sharing class Describer
{
public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
public List<Pair> lstfieldname{get;set;}
public List <Pair> fields {get{return lstfieldname;} set{lstfieldname =value;}}

// Intialize objectNames and fields
public Describer() {
    fields = new List<Pair>();
    showFields();
}

// Find the fields for WRAP Report Object

public void showFields() {

Map <String, Schema.SObjectField> fieldMap = schemaMap.get('Sample__c').getDescribe().fields.getMap();
for(Schema.SObjectField sfield : fieldMap.Values())
    {
        schema.describefieldresult dfield = sfield.getDescribe();
        system.debug('#######' + dfield );
        Pair field = new Pair();
        field.key = dfield.getname();
        system.debug('#######4444' + field.key);
        field.val =dfield.getLabel ();
        lstfieldname.add(field);
    }
}

public class Pair
{
    public String key {get; set;}
    public String val {get; set;}
}
}

Visualforce page
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
<apex:page Controller="Describer">
    <apex:form id="Describe">
        <apex:pageBlock title="Salesforce Field">
            <apex:pageBlockTable value="{!fields}" var="fls" styleClass="table table-striped">
                <apex:column value="{!fls.val}"/>
                <!-- Header Generated automatically -->
            </apex:pageBlockTable>
        </apex:pageBlock>
    </apex:form>
</apex:page>
--------------------------------------------------------------------------------------------------------------------------------------------------------
The result was this

Name
Address
Gender
Age
Nationality

1. I need some kind of explanation about the flow of the class code (I know what the code does but i'm not familiar with most parts of it).
2. How do I sort the results alphabetically (to make it easier to locate a field)?
 
Best Answer chosen by keshin okawa
Shrikant BagalShrikant Bagal
@miczster masher,
For explanation please refer: 
 -  https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_dynamic_describeSObject.htm
 -   https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_dynamic_vf_globals_objecttype.htm
and about sorting have you try for sort function of List.