Last active
August 29, 2015 14:07
-
-
Save arufian/6395de554f93a8adb9c5 to your computer and use it in GitHub Desktop.
Select * in SOQL using Apex Class
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
String table = 'TableName'; // with __c if use Custom Object | |
Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe() ; | |
Schema.SObjectType s = m.get(table) ; | |
Schema.DescribeSObjectResult r = s.getDescribe() ; | |
Map<String, Schema.SObjectField> fields = r.fields.getMap() ; | |
string soql = ''; | |
for (String fieldName : fields.keyset()) { | |
if (soql != '') { | |
soql += ', '; | |
} | |
soql += fieldName; | |
} | |
soql = 'SELECT ' + soql + ' FROM ' + table; | |
System.debug(soql); | |
List<TableName> acs = Database.query(soql); | |
System.debug(acs.get(0)); |
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
/** | |
* Bring Select * into SOQL | |
* @param tableName tableName | |
* @param whereCondition cover WHERE, GROUP BY, ORDER BY, AND LIMIT | |
* @param isCustomFieldOnly set true if only to get custom fields | |
* @return String string concat query | |
**/ | |
public static String getSelectAllQuery(String tableName, String whereCondition, Boolean isCustomFieldOnly) { | |
Map<String, Schema.SObjectType> m = Schema.getGlobalDescribe() ; | |
Schema.SObjectType s = m.get(tableName) ; | |
Schema.DescribeSObjectResult r = s.getDescribe() ; | |
Map<String, Schema.SObjectField> fields = r.fields.getMap() ; | |
string soql = ''; | |
for (String fieldName : fields.keyset()) { | |
if (soql != '') { | |
soql += ', '; | |
} | |
if(isCustomFieldOnly && (fieldName.indexOf('__c') < 0)) { | |
continue; | |
} | |
soql += fieldName; | |
} | |
soql = 'SELECT ' + soql + ' FROM ' + tableName + ' ' + whereCondition; | |
return soql; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment