0

How to make such datatable, that will fetch all the columns in my custom object with particular data type and, when I add a new field to my object, the table will automatically fetch this field and make a new column for it without updating the code?

1

Unless you absolutely need it - I won't recommend it. This is kind of common use case, but code is complex. You need to reference SObject Schema in order to get fields info and query those fields and pass it into results.

Here is a small part of the code, since you also need to handle different field Types and add some code for querying your Object.

I can also found a video for that thing.

Map<String, SObjectField> fieldsMap = SObjectType.Some_Type__c.fields.getMap();
Set<String> fields = new Set<String>();

for (String fieldName : fieldsMap.keySet()) {
    SObjectField field = fieldsMap.get(fieldName);
    if (field.getDescribe().isAccessible()) {
        fields.add(fieldName);
    }
}


String query = '';
// Build query for your SObject

List<SObject> records = Database.query(query);

List<Object> result = new List<Object>();
// Iter through records and wrap your info

return new Map<String, Object>{
        'fields' => fields,
        'data' => result
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.