Skip to content

Instantly share code, notes, and snippets.

@cmbaughman
Last active May 11, 2017 06:01
Show Gist options
  • Save cmbaughman/47382de6919cee59aa9b to your computer and use it in GitHub Desktop.
Save cmbaughman/47382de6919cee59aa9b to your computer and use it in GitHub Desktop.
Salesforce Apex Query with Wildcard
public static String getAllQuery(String query) {
String result = '';
String regex = '^select\\s+\\*\\s*(?:,\\s*[^\\s]+\\s*)*\\s+from\\s+([^\\s]+)(.*)$';
Matcher m = Pattern.compile(regex).matcher(query.toLowerCase());
if(m.matches()) {
String sObjectName = m.group(1);
Schema.SObjectType targetType = Schema.getGlobalDescribe().get(sObjectName);
Map<String, Schema.SObjectField> fieldMap = targetType.getDescribe().fields.getMap();
List<String> fieldList = new List<String>();
fieldList.addAll(fieldMap.keyset());
String allFields = String.join(fieldList, ', ');
System.debug('Have field list: ' + allFields);
result = query.replace('*', allFields);
}
else {
result = query;
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment