Activity Forums Salesforce® Discussions Dynamic Query via Visualforce Page

  • Himanshu

    Member
    August 1, 2016 at 11:25 am

    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.

Log In to reply.

Popular Salesforce Blogs

Popular Salesforce Videos