5

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.

Necreaux
  • 9,451
  • 7
  • 26
  • 43
Usman Ali
  • 93
  • 1
  • 3
  • 10

4 Answers4

1

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.

Necreaux
  • 9,451
  • 7
  • 26
  • 43
Ali Azam
  • 2,047
  • 1
  • 16
  • 25
0

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.

Daniel Ballinger
  • 13,187
  • 11
  • 69
  • 96
0

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

Guest
  • 1
0

This SOQL query does not have a limit of 200:

SELECT Label,
       QualifiedApiName
  FROM FieldDefinition
 WHERE EntityDefinition.QualifiedApiName = 'Account'