組建SOQL時常需要自行輸入欄位名稱,欄位一多就會耗費許多時間,
以下function可一次取出所有欄位組成SOQL字串
傳入SOject Name可取得所有欄位名稱 (以逗點隔開)
public getQuote(){
string SOQL = 'select ' + GetAllField('Quote') + ' from Quote ';
list<Quote> QuoteList = Database.query(SOQL);
}
public string GetAllField(string sfo){
map<string, schema.sobjecttype> allSObjects = schema.getglobaldescribe();
schema.sobjecttype q = allsobjects.get(sfo);
schema.describesobjectresult d = q.getdescribe();
map<string, schema.sobjectfield> m = d.fields.getmap();
set<string> s = m.keyset();
string query = '';
for(string f : s){
query = query+f+', ';
}
query = query.substring(0,query.length()-2);
return query;
}
沒有留言:
張貼留言