Hi Mohit,
Below is the code of fetching data via dynamic query and shows on the visualforce page.
public class selectAllSOQLExampleController {
public List accList{get;set;}
public String query{get;set;}
public selectAllSOQLExampleController(){
}
public PageReference fetch(){
String SobjectApiName = ‘Account’;
Map schemaMap = Schema.getGlobalDescribe();
Map fieldMap = schemaMap.get(SobjectApiName).getDescribe().fields.getMap();
String commaSepratedFields = ”;
for(String fieldName : fieldMap.keyset()){
if(commaSepratedFields == null || commaSepratedFields == ”){
commaSepratedFields = fieldName;
}else{
commaSepratedFields = commaSepratedFields + ‘, ‘ + fieldName;
}
}
query = ‘select ‘ + commaSepratedFields + ‘ from ‘ + SobjectApiName + ‘ Limit 5’;
accList = Database.query(query);
return null;
}
}
Hope this helps you.