Created
July 27, 2018 14:26
-
-
Save sfcure/81888ea3d0038b8f14df7c2006d98d7a to your computer and use it in GitHub Desktop.
Creating a dynamic SOQL which includes all the fields
This file contains hidden or 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
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