Skip to content

Instantly share code, notes, and snippets.

@austinogilvie
Created November 20, 2014 22:47
Show Gist options
  • Select an option

  • Save austinogilvie/fa03a2d85cbe2c317a0d to your computer and use it in GitHub Desktop.

Select an option

Save austinogilvie/fa03a2d85cbe2c317a0d to your computer and use it in GitHub Desktop.
Salesforce won't let you select star (SELECT *). This is a workaround where you generate the SOQL manually by inspecting the schema of the table.
Map<String, Schema.SObjectField> field_objmap = schema.SObjectType.Opportunity.fields.getMap();
List<Schema.SObjectField> field_objmap_values = field_objmap.values();
List<String> field_names_list = new List<String>();
for(Schema.SObjectField s : field_objmap_values) {
String field_label = String.valueOf(s.getDescribe().getLabel()); // Perhaps store this in another map
String field_name = String.valueOf(s.getDescribe().getName());
String field_type = String.valueOf(s.getDescribe().getType());
field_names_list.add(field_name);
}
String q = 'SELECT ';
q += String.join(field_names_list, ',');
q += ' FROM Opportunity WHERE Name = \'foobar\'';
// print the query
System.debug(q);
Opportunity opp = Database.query(q);
// confirm it worked
System.debug(opp.Name);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment