Skip to content

Instantly share code, notes, and snippets.

@douglascayers
Last active December 2, 2020 14:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save douglascayers/7d4d3446125a17b7d605b1d9441bb359 to your computer and use it in GitHub Desktop.
Save douglascayers/7d4d3446125a17b7d605b1d9441bb359 to your computer and use it in GitHub Desktop.
Example SOQL to query for all fields using Dyanamic Query in Apex
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 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment