In the Spring ’21 release, you can now select all fields more easily using the FIELDS() keyword. The approach in this blog post is preserved for historical reference.
SOQL is Salesforce’s object query language. It has familiar basic syntax to traditional database SQL syntax but with some noticeable differences.ย One of the biggest differences is that SOQL does not support wildcard in SELECT statements to query all fields from an object. For example, “SELECT * FROM object”.ย However, there is a workaround by using Dynamic SOQL and Apex Describe.
In the below example I show you how to query all fields from an object by exploiting the metadata available from the DescribeSObjectResult class.
Example 1: Query All Fields for a Known Record ID
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ID recordId = '5001a00000CgCE2'; | |
DescribeSObjectResult describeResult = recordId.getSObjectType().getDescribe(); | |
List<String> fieldNames = new List<String>( describeResult.fields.getMap().keySet() ); | |
String query = | |
' SELECT ' + | |
String.join( fieldNames, ',' ) + | |
' FROM ' + | |
describeResult.getName() + | |
' WHERE ' + | |
' id = :recordId ' + | |
' LIMIT 1 ' | |
; | |
// return generic list of sobjects or typecast to expected type | |
List<SObject> records = Database.query( query ); | |
System.debug( records ); |
Example 2: Query All Fields for All Records
The same concept as above but we don’t have a specific record ID, so we need to determine the sobjectype by explicitly specifying the object we’ll query on.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// without an ID, simply specify the object to then derive the sobject type | |
DescribeSObjectResult describeResult = Account.getSObjectType().getDescribe(); | |
List<String> fieldNames = new List<String>( describeResult.fields.getMap().keySet() ); | |
String query = | |
' SELECT ' + | |
String.join( fieldNames, ',' ) + | |
' FROM ' + | |
describeResult.getName() | |
; | |
// return generic list of sobjects or typecast to expected type | |
List<SObject> records = Database.query( query ); | |
System.debug( records ); |
Next Steps
In addition to the resources linked above, check out these great tutorials to continue learning more about SOQL!
I tried these queries, but errors indicate that List syntax requires the sObjectType like so:
// without an ID, simply specify the object to then derive the sobject type
DescribeSObjectResult describeResult = Account.getSObjectType().getDescribe();
List fieldNames = new List( describeResult.fields.getMap().keySet() );
String query =
‘ SELECT ‘ +
String.join( fieldNames, ‘,’ ) +
‘ FROM ‘ +
describeResult.getName()
;
// return generic list of sobjects or typecast to expected type
List records = Database.query( query );
System.debug( records );
Event then I get an error I’m not sure how to fix:
line 3, col 28. Invalid initial value type Set for List
LikeLiked by 1 person
Hi Tim,
Sorry about that, it seems my blog has stripped out the < and > characters thinking they were HTML tags and not code. Doh!
Will get that fixed shortly.
Thanks,
Doug
LikeLike
Nice. Thanks!
LikeLiked by 1 person
Hey Doug this is awesome was wondering does this only pull back so many custom fields? I ran this in anonymous apex and only received a few of my custom fields.
LikeLiked by 1 person
Hi Bryant, glad you like it!
I’m not aware of any field limitations. Confirm that your user has read access to each field. Developer Console uses the Tooling REST API to execute Apex and respects sharing and access.
Thanks,
Doug
LikeLike
Thanks for the great code Doug!! Saved me a bunch of time.
LikeLiked by 1 person
Thanks for the kind feedback, Jeff!
LikeLike
thanks Doug, It worked wonderfully.
LikeLiked by 1 person