Select All Fields of a Salesforce Object Using SOQL
Asked Answered
M

4

7

I want to do something that could be done in SQL:

select * from table

How can this be achieved using SOQL?

I won't be able to use Apex Code, because I run SOQL queries from a PHP page.

Matriarchate answered 20/6, 2014 at 11:37 Comment(0)
A
2

By default SOQL has no support for selecting all fields in this way:
SELECT * From sObject

Check below for a generic class that accepts the object name, and based on that gives a SOQL query for selecting all fields as a String.

Apex Class:

public inherited sharing class GenerateDynamicSOQLQuery {

    public static string getSOQLQuery(String strObjectName) {
        if(!String.isBlank(strObjectName)) {
            String strQuery = 'SELECT ';
            list<Schema.DescribeSObjectResult> objDiscribe = Schema.describeSObjects(new List<String>{strObjectName});
            map<String, Schema.SObjectField> objFileds = objDiscribe[0].fields.getMap();
            list<String> lstOfFieldNames = new list<String>(objFileds.keySet());
            strQuery = strQuery + String.join(lstOfFieldNames, ', ') + ' FROM ' +strObjectName;
            return strQuery;
        }
        else {
            return null;
        }
    }
}

Demo: To obtain the output of the generated query, open an "Execute Anonymous" window and then execute below code:

String strQuery = GenerateDynamicSOQLQuery.getSOQLQuery('Account');
//strQuery += ' WHERE Industry = \'Banking\' LIMIT 1';
strQuery += ' LIMIT 1';
System.debug('Account Records ====>  '+Database.query(strQuery));

The query results will be in the debug logs.

Advisory answered 3/10, 2019 at 5:48 Comment(0)
O
1

You can use the meta data available from the API to determine which fields for the object are available to the current user.

See the PHP Toolkit 20.0 DescribeSObject Sample for the Enterprise or Partner WSDL. You want the Name from the fields array. Append them together with comma separators to make the complete list of accessible fields.

Odessa answered 21/6, 2014 at 8:37 Comment(0)
M
1

You can use FIELDS() as the complete field list. For example:

SELECT FIELDS(ALL) FROM Account LIMIT 200
SELECT FIELDS(CUSTOM) FROM Account LIMIT 200
SELECT FIELDS(STANDARD) FROM Account

You can also mix FIELDS() with other field names in the field list. For example:

SELECT Name, Id, FIELDS(CUSTOM) FROM Account LIMIT 200
SELECT someCustomField__c, FIELDS(STANDARD) FROM Account

Note: Max Limit is 200 - see documentation here

Mcguigan answered 29/3, 2021 at 22:32 Comment(0)
S
0

This SOQL query does not have a limit of 200:

SELECT Label,
       QualifiedApiName
  FROM FieldDefinition
 WHERE EntityDefinition.QualifiedApiName = 'Account'
Sotelo answered 10/5, 2023 at 18:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.