-
-
Save 402332509/5cbd296372cf2f51108f3b79b8360229 to your computer and use it in GitHub Desktop.
Salesforce Apex Query with Wildcard
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 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