function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sancarlos94070sancarlos94070 

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!!
wesnoltewesnolte

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 

 

 

Message Edited by wesnolte on 06-23-2009 01:56 AM