Dynamic SOQL query select all fields in Salesforce

Dynamic SOQL query Salesforce

soql select all

Dynamic SOQL query Salesforce for soql select all. SOQL is Salesforce Object Query Language for querying data in the Force.com platform. It is very much similar to SQL.

But in SOQL, we can not query all fields from object. This statement is not allowed in SOQL:

select * from Account;

But there is one trick to query all fields of Object in SOQL query.

In the example below, we will use SOQL query to fetch all fields of account. We have added one ‘Fetch Account’ button on page. When we will click on this button all fields of account objects are fetched using SOQL query. We have also shown SOQL query in Visualforce page result.

Click for Demo

Visualforce Code:

<apex:page controller="selectAllSOQLExampleController" tabStyle="Account">
    <apex:form >
        <apex:pageBlock >
            <apex:pageBlockButtons >
                <apex:commandButton value="Fetch Accounts" action="{!fetch}"/>
            <apex:pageBlockSection columns="1" title="Dynamic SOQL Query" collapsible="false">
                <apex:outputText value="{!query}">

            <apex:pageBlockSection title="Account table" columns="1" collapsible="false">
                <apex:pageBlockTable value="{!accList}" var="acc">
                    <apex:column value="{!acc.name}"/>
                    <apex:column value="{!acc.phone}"/>
                    <apex:column value="{!acc.rating}"/>
                    <apex:column value="{!acc.industry}"/>
                    <apex:column value="{!acc.accountnumber}"/>


Apex Code:

public class selectAllSOQLExampleController {
    public List<Account> accList{get;set;}
    public String query{get;set;}
    public selectAllSOQLExampleController(){
    public PageReference fetch(){
        String SobjectApiName = 'Account';
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Map<String, Schema.SObjectField> fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();

        String commaSepratedFields = '';
        for(String fieldName : fieldMap.keyset()){
            if(commaSepratedFields == null || commaSepratedFields == ''){
                commaSepratedFields = fieldName;
                commaSepratedFields = commaSepratedFields + ', ' + fieldName;

        query = 'select ' + commaSepratedFields + ' from ' + SobjectApiName + ' Limit 5';

        accList = Database.query(query);

        return null;

Good luck for Dynamic SOQL query Salesforce or for SOQL select all fields. For other similar post please refer this link.

Permanent link to this article: http://www.sfdcpoint.com/salesforce/dynamic-soql-query-fetch-fields-object-salesforce/


    • k mahesh on June 25, 2014 at 9:58 am
    • Reply

    Hi ,
    Is it possible Dynamically querying fields related information display in a pageblock table.

    help me……

    • Nilesh on December 1, 2014 at 3:49 pm
    • Reply

    what about new Fields on VF page ????? If I have created “myCustomField__C” , I get that this is Queried in SOQL Query, but what about displaying this on VF page dynamically ?

    1. For displaying newly created field in visualforce page. You can use fieldset. You can check following link http://www.sfdcpoint.com/salesforce/fieldset-visualforce-page-salesforce/

Leave a Reply

Your email address will not be published.