Skip to content

Instantly share code, notes, and snippets.

@suddeb
Last active October 3, 2016 00:12
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 suddeb/df30bb516abc2cafc4e536f24e55ff26 to your computer and use it in GitHub Desktop.
Save suddeb/df30bb516abc2cafc4e536f24e55ff26 to your computer and use it in GitHub Desktop.
DynamicSOQLPractice
public with sharing class DynamicSOQLPractice {
public static void getAccountFieldsDynamically(){
String allFields = '';
List<Account> allAccounts;
Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.Account.fields.getMap();
for(Schema.SObjectField singleField : fieldMap.values()){
allFields = allFields + ',' + singleField.getDescribe().getName();
}
//Remove the first ',' from allFields
allFields = allFields.subString(1,allFields.length());
System.Debug('-- FIELDS: ' + allFields);
String queryString = 'SELECT ' + allFields + ' FROM ACCOUNT';
System.Debug('-- QUERY: ' + queryString);
if(!String.isBlank(queryString)){
allAccounts = Database.query(queryString);
}
if(allAccounts != null && !allAccounts.isEmpty()){
System.Debug('-- ACCOUNT FETCHED: ' + allAccounts.size());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment