Skip to content

Instantly share code, notes, and snippets.

@sfcure
Created July 27, 2018 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sfcure/81888ea3d0038b8f14df7c2006d98d7a to your computer and use it in GitHub Desktop.
Save sfcure/81888ea3d0038b8f14df7c2006d98d7a to your computer and use it in GitHub Desktop.
Creating a dynamic SOQL which includes all the fields
Map<String,Schema.SObjectField> mapFields = Account.getSObjectType().getDescribe().fields.getMap();
List<String> fieldNames = new List<String>();
for( String fieldName : mapFields.keySet() ) {
fieldNames.add( fieldName );
// For relationship fields
if( Schema.DisplayType.Reference == mapFields.get(fieldName).getDescribe().getType() )
fieldNames.add( mapFields.get(fieldName).getDescribe().getRelationshipName() + '.Name' );
}
String query =
' SELECT ' +
String.join( fieldNames, ',' ) +
' FROM Account ' +
' WHERE ' +
' Id = :recorId ';
List<Account> lstRecords = Database.query( query );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment