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
ArchestainArchestain 

how to check what are all the fields available in table/object by using SOQL similar to desc table name in SQL?

Dilip_VDilip_V
Hi Archestain,

To know the fields we have to use apex schema programming.You have to use this class Schema.SObjectField
here is the detailed documentation link
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_methods_system_schema.htm

Here is sample page and controller if you select the object then related fields will be displayed.Please save the page and try you will understand clearly.
Controller:
public with sharing class DescibeDemoController 
{
    public Map <String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
    public String selectedObject {get; set;}
    public List<FieldWrapper> listField{get;set;}

    public DescibeDemoController() 
    {
        listField = new List<FieldWrapper>();
    }

    // find all sObjects available in the organization
    public  List<SelectOption> getListObejectName() 
    {
        List<SelectOption> objNames = new List<SelectOption>();
        List<String> entities = new List<String>(schemaMap.keySet());
        entities.sort();
        for(String name : entities)
            objNames.add(new SelectOption(name,name));
        return objNames;
    }

    
    // Find the fields for the selected object
    public void showFields() 
    {
        listField.clear();
        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(selectedObject).getDescribe().fields.getMap();
        for(Schema.SObjectField sfield : fieldMap.Values())
        {
            schema.describefieldresult dfield = sfield.getDescribe();
            FieldWrapper wObj = new FieldWrapper();
            wObj.fieldName = dfield.getLabel ();
            wObj.fieldAPIName = dfield.getname();
            listField.add(wObj);
        }
    }

    public class FieldWrapper
    {
        public String fieldName {get; set;}
        public String fieldAPIName {get; set;}
    }

}

Apex page:
 
<apex:page controller="DescibeDemoController">
    <apex:form id="Describe">
        <apex:pageBlock id="block2" >
            <apex:pageblockbuttons location="top" >
                    <apex:commandButton value="Show Fields" action="{!showFields}" />
            </apex:pageblockbuttons>
            
            <apex:pageblocksection >
                <apex:pageBlockSectionItem >
                    <apex:outputLabel >Object Name</apex:outputLabel>
                    <apex:selectList value="{!selectedObject}" size="1">
                        <apex:selectOptions value="{!ListObejectName}"/>
                    </apex:selectList>
                </apex:pageBlockSectionItem>
            </apex:pageblocksection>
        </apex:pageBlock>
        
        <apex:pageBlock id="result" title="Field Detail for {!selectedObject}" rendered="{!if(listField.size > 0 ,true,false)}"   >
            <apex:pageBlockTable value="{!listField}" var="field" rendered="{!if(listField.size > 0 ,true,false)}"> 
                <apex:column value="{!field.fieldName }" headerValue="Name" />
                <apex:column value="{!field.fieldAPIName }"  headerValue="API Name"/>
            </apex:pageblockTable>
        </apex:pageblock>
    </apex:form>
</apex:page>

If it's helpful please make it best answer.

Thanks,
Dilip.
ArchestainArchestain
Hi Dilip,

Thanks for your solution. Can you please provide steps after how to execute this apex page. I had saved the apex page and im clueless about the execution part and please provide your POV, if this can be acheived via the query editor?

Regards
Archestain