Last active
October 3, 2016 00:12
-
-
Save suddeb/df30bb516abc2cafc4e536f24e55ff26 to your computer and use it in GitHub Desktop.
DynamicSOQLPractice
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
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