You need to sign in to do that
Don't have an account?
sancarlos94070
Looking for example that will dynamically create SOQL to select all the fields of a Lead object
I'm looking for Apex example to illustrate how to dynamically create a SOQL query to select all the fields of a Lead object. Can anyone point me to an example? Tried the search feature here & Apex doc with no luck.
Thank you!!
Thank you!!
Hey
Something like this should work
String qs = 'SELECT ';
Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); // map of all objects
Schema.SObjectType s = gd.get('Lead'); // our object
Schema.DescribeSObjectResult dor = s.getDescribe(); // description of our object
Map<String, Schema.SObjectField> m = dor.fields.getMap(); // map of all our object fields
Set<String> fields = m.keySet();
for(Integer i=0; i<fields.size();i++){
if(i=field.size()-1) // if it's the last field don't append a comma
qs += field;
else
qs += field +',';
}
qs += 'FROM Lead';
database.query(qs);
I typed this code straight into the browser so there may be some mistakes but this is the gist of it.
CHeers,
Wes