Jan 21

SOQL Select all fields (Select * from )

public class ApexSelectQueries {
//SOQL will not support direct select * from syntax, so we need to retrive all field names first then we should construct a select statment with the fields.
//This method generates a SOQL select statement equal to select * from object.
//if your object is Account then your fieldMap should be return type of this statment schema.SObjectType.Account.fields.getMap();
//The return type of this method is string i.e select field1,field2,… from yourObject.
public String selectAll(Map fieldMap,String yourObject)
{
Map fldObjMap = fieldMap;
List fldObjMapValues = fldObjMap.values();
String theQuery=”;
for(Schema.SObjectField sObjField : fldObjMapValues)
{
String theName = sObjField.getDescribe().getName();
theQuery += theName+’,';
}
if(theQuery.lastIndexOf(‘,’)!=-1)
{
theQuery = theQuery.removeEnd(‘,’);
}
theQuery = ‘SELECT ‘+theQuery+’ From ‘+yourObject;
return theQuery.trim();
}
}