Skip to content

Instantly share code, notes, and snippets.

@darshilv
Last active December 20, 2015 16:19
Show Gist options
  • Save darshilv/6160340 to your computer and use it in GitHub Desktop.
Save darshilv/6160340 to your computer and use it in GitHub Desktop.
Function to query all the fields of an SObject in Salesforce
Map<String, Schema.SObjectField> fieldMap = Schema.SObjectType.<<Object_API_Name>>.fields.getMap();
//constructing a set of fieldnames
Set<String> keySet = fieldMap.keySet();
String soql = 'SELECT ';
for(String x : keySet) {
//this might be avoidable - will look at it in later version but currently pulls in the API Field name
Schema.DescribeFieldResult r = fieldMap.get(x).getDescribe();
//appending the fieldname to the query string
soql += r.getName() + ', ';
}
//removing the extra comma and space from the end of the query
soql = soql.substring(0, soql.length()-2);
// providing the name of the SObject and any additional clauses
soql += ' FROM <<Object_Api_Name>>';
<<Object_Api_Name>> template = (<<Object_Api_Name>>)Database.query(soql);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment