Below is the sample code to display a dynamic table, which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
public class ObjectPicklistController1 {
public String selectedObject {get;set;}
public List<sObject> objFields {get;set;}
public List<String> result {get;set;}
public ObjectPicklistController1(){
selectedObject = 'none';
}
public List<SelectOption> getobjectNames(){
List<Schema.SObjectType> gd = Schema.getGlobalDescribe().Values();
List<SelectOption> options = new List<SelectOption>();
for(Schema.SObjectType f : gd){
options.add(new SelectOption(f.getDescribe().getName(),f.getDescribe().getName()));
options.sort();
}
return options;
}
public void fetchFields(){
result = new List<String>();
Map<String , Schema.SObjectType> globalDescription = Schema.getGlobalDescribe();
Schema.sObjectType objType = globalDescription.get(selectedObject);
Schema.DescribeSObjectResult r1 = objType.getDescribe();
Map<String , Schema.SObjectField> mapFieldList = r1.fields.getMap();
for(Schema.SObjectField field : mapFieldList.values()){
Schema.DescribeFieldResult fieldResult = field.getDescribe();
if(fieldResult.isAccessible()){
result.add(fieldResult.getName());
}
}
Integer i = 0;
String fieldsToFetch = '';
for(String temp:result){
Integer len = result.size();
if(i==len-1){
fieldsToFetch = fieldsToFetch + temp;
}
else{
fieldsToFetch = fieldsToFetch + temp + ',';
}
i++;
}
try{
String sql = ' SELECT ' + fieldsToFetch + ' FROM ' + selectedObject + ' ORDER BY CreatedDate DESC LIMIT 5';
objFields = Database.Query(sql);
}
catch(Exception e){
ApexPages.addMessages(e);
}
}
}
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Hey , can you help me customise the columns that i am displaying,i have made a custom setting named:General which has two fields,Objectname and Apiname,and the records have the list of fields i want to show,problem is i cant figure out how to query it in my table header.it would be a great help !
Greetings to you!
Below is the sample code to display a dynamic table, which I have tested in my org and it is working fine. Kindly modify the code as per your requirement.
1. LIGHTNING CODE:
Application:
Component:
Controller:
CSS:
Apex:
2. VISUALFORCE CODE:
Visualforce:
Controller:
I hope it helps you.
Kindly let me know if it helps you and close your query by marking it as solved so that it can help others in the future. It will help to keep this community clean.
Thanks and Regards,
Khan Anas
can you help me customise the columns that i am displaying,i have made a custom setting named:General which has two fields,Objectname and Apiname,and the records have the list of fields i want to show,problem is i cant figure out how to query it in my table header.it would be a great help !