Thursday, June 9, 2016

How To query all Fields in Salesforce ?

1- Create a Class Called QueryClass  where  we create a simple static queryMethod with parameter.

 public with sharing class QueryClass {
public static string queryMethod(string Obj){
  
      Map<String, schema.sobjectType> sobjType= schema.getglobaldescribe();
schema.sobjectType findObj = sobjType.get(Obj);
schema.describesobjectresult dor = findObj .getdescribe();
map<string, schema.sobjectfield> sObjField = dor.fields.getmap();
set<string> str = sObjField .keyset();
String query = '';
for(string addfields: str){
query = query+addfields+', ';
}
query = query.substring(0,query.length()-2);
String allfields = query;

String soql = 'SELECT ' + query + ' FROM '+Obj; 
        return soql;
  }

}

2-Now we just have to call this method from any class,triggers etc.

String soqlData= QueryClass .queryMethod('Contact');

List<Contact> con=database.query(soqlData);

This will return a list of contact record with all the Fields

No comments:

Post a Comment

Common interview questions for Salesforce developers

Name three Governor Limits .                                                                                    Description Synchronous ...