/*
* Class Name : BuildQueryUtil
* Last Modified Date : 22-01-2014
* Created By : Gautam Singh
* Class Type : Utlity
* Purpose : Add this excerpt in your utility class to save your queries from
* Error: SObject row was retrieved via SOQL without querying the requested field: [field_name]
*
*/
public class BuildQueryUtil{
//constructor
public BuildQueryUtil(){
//your own utility initialzations
}
/*
*
* Purpose: Returns the fieldNames comma seperated.
* Note: Reason for Not Building Query & sending the field names is: You may want to extract child/parent fields as well!
*
*
*/
public static string fieldNames(string objtName){
string query = \'SELECT\';
string ObjectName = ObjtName;
map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(objectName).getDescribe().fields.getMap();
// create the query string by getting field api names from the map.
for(String s : objectFields.keySet()) {
query += \' \' + s + \', \';
}
// remove comma from last, if it exsists
// if you are planning to add nested query it is recommended that you keep the comma
if (query.subString(query.Length()-1,query.Length()) == \',\'){
query = query.subString(0,query.Length()-1);
query += \' \';
}
return query;
}
}