Created
March 23, 2019 19:24
-
-
Save sfmishra/e5d26ab0012563dcd5bb9fa5b50d8a17 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 class UtilityClass { | |
public static List<sObject> queryObjects(String theObject, List<String> theFields, List<String> theFilters, String sortField, String sortOrder) { | |
String theQuery = 'SELECT ' + string.join(theFields, ','); | |
theQuery += ' FROM ' + theObject; | |
boolean firstFilter = true; | |
for(String filter : theFilters) { | |
String clauseToUse = (firstFilter) ? ' WHERE ' : ' AND '; | |
filter = filter.trim(); | |
filter = filter.replaceAll('(\\s+)', ' '); // removing white spaces | |
theQuery += clauseToUse + filter; | |
} | |
if(!String.isEmpty(sortField)) { | |
theQuery += ' ORDER BY ' + sortField; | |
if(!String.isEmpty(sortOrder)) { | |
theQuery += ' ' + sortOrder; | |
} | |
} | |
String queryResult = string.escapeSingleQuotes(theQuery); | |
return Database.query(queryResult); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment