function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Leuwend Job HapaLeuwend Job Hapa 

Is it possible to display records in a datatable with the queried columns dynamically.

Assuming that we have a list of Account objects from the database:
List<Account> listOfAccounts = new List<Account>();

for (Account a : [SELECT Id, Name, Type FROM Account LIMIT 10]) {
    listOfAccounts.add(a);
}
I want to display it on a datatable that automatically adds the columns that I have queried (ID, Name, and Type).
Best Answer chosen by Leuwend Job Hapa
Hargobind_SinghHargobind_Singh
Yes, it is possible. You would need to:
  1. Retreive field names from your query result and store them in a separate array
  2. Create a List<List<String>> collection and in the first row, store your field-names, and in subsequent rows, store values from your sObject list. 
  3. Display this List<List<String>> collection in your VF page in a table. 
Here is some code that has been taken from an article that talks about how to retrieve field names, basicaly, this converts the query-result to jSON and parses it to get field-names, you can use the same code to retreive field-values as well. Article URL: http://salesforce.stackexchange.com/questions/24336/how-do-i-get-the-list-of-fields-of-sobject

public with sharing class QueryQueriedFields 
{
    public static void demo()
    {
        Account account = [select Id, AnnualRevenue, Website, Owner.Username from Account limit 1];
        Map<String, Object> queriedFieldValues = (Map<String, Object>) JSON.deserializeUntyped(JSON.serialize(account));
        dumpFields('', queriedFieldValues);
    }

    public static void dumpFields(String relatedField, Map<String, Object> queriedFieldValues)
    {
        for(String queriedFieldName : queriedFieldValues.keySet())
        {
            // Skip this information, its not a field
            if(queriedFieldName.equals('attributes'))
                break;
            // Check if the queried value represents a related field reference?
            Object queriedFieldValue = queriedFieldValues.get(queriedFieldName);
            if(queriedFieldValue instanceof Map<String,Object>)
                dumpFields(queriedFieldName + '.', (Map<String, Object>) queriedFieldValue);
            else
                System.debug(relatedField + queriedFieldName + ' = ' + queriedFieldValue);
        }       
    }
}


All Answers

Hargobind_SinghHargobind_Singh
Yes, it is possible. You would need to:
  1. Retreive field names from your query result and store them in a separate array
  2. Create a List<List<String>> collection and in the first row, store your field-names, and in subsequent rows, store values from your sObject list. 
  3. Display this List<List<String>> collection in your VF page in a table. 
Here is some code that has been taken from an article that talks about how to retrieve field names, basicaly, this converts the query-result to jSON and parses it to get field-names, you can use the same code to retreive field-values as well. Article URL: http://salesforce.stackexchange.com/questions/24336/how-do-i-get-the-list-of-fields-of-sobject

public with sharing class QueryQueriedFields 
{
    public static void demo()
    {
        Account account = [select Id, AnnualRevenue, Website, Owner.Username from Account limit 1];
        Map<String, Object> queriedFieldValues = (Map<String, Object>) JSON.deserializeUntyped(JSON.serialize(account));
        dumpFields('', queriedFieldValues);
    }

    public static void dumpFields(String relatedField, Map<String, Object> queriedFieldValues)
    {
        for(String queriedFieldName : queriedFieldValues.keySet())
        {
            // Skip this information, its not a field
            if(queriedFieldName.equals('attributes'))
                break;
            // Check if the queried value represents a related field reference?
            Object queriedFieldValue = queriedFieldValues.get(queriedFieldName);
            if(queriedFieldValue instanceof Map<String,Object>)
                dumpFields(queriedFieldName + '.', (Map<String, Object>) queriedFieldValue);
            else
                System.debug(relatedField + queriedFieldName + ' = ' + queriedFieldValue);
        }       
    }
}


This was selected as the best answer
Vatsal KothariVatsal Kothari
HI,

you can refer below link:

http://www.salesforce.com/us/developer/docs/pages/Content/pages_compref_dataTable.htm

If this solves your problem, kindly mark it as the best answer.

Thanks,
Vatsal