Retrieving all SObject fields!
At some point you may require all the fields for an object and display data only for specific feilds and this may change
with the requirement. A usecase would be in Lightning DataTables. Querying Schema class is the solution but its a
whole deep pool of maps, wherein you need to drill down deep. Here, I will share my technique.
I like to combine it the Singleton pattern and boom we have a cocktail of good oops practice.
class Utility{
private static Set<String> fieldList;
public Set<String> getAllFields(String objName){
if(fieldList == NULL){
Map<String, Schema.SobjectType> schemaMap = Schema.getGlobalDescribe();
Schema.SObjectType objectSchema = schemaMap.get(objName);
Map<String, Schema.SObjectFields> fieldsMap = objectSchema.getDescribe().getFields.getMap();
fieldsList = fieldsMap.keySet();
return fieldList;
}
return fieldList;
}
}