Skip to content

Instantly share code, notes, and snippets.

@nithesh1992
Last active December 12, 2016 20:02
Show Gist options
  • Save nithesh1992/8a2348fb6797b8c823ce06c2202546b6 to your computer and use it in GitHub Desktop.
Save nithesh1992/8a2348fb6797b8c823ce06c2202546b6 to your computer and use it in GitHub Desktop.
utils class to get a list of all fields of an object, all required fields of an object etc... and more to come
public class metaDataUtils {
public static Map<String, Schema.DescribeFieldResult> getFieldMetaData(String obj) {
Map<String,Schema.DescribeFieldResult> finalMap = new Map<String, Schema.DescribeFieldResult>();
Map<String, Schema.SObjectField> objectFields = Schema.getGlobalDescribe().get(obj).getDescribe().fields.getMap();
// getting a specific object fields
for(String fieldName: objectFields.keySet()){
finalMap.put(fieldName, objectFields.get(fieldName).getDescribe());
}
return finalMap;
}
public static List<String> getRequiredFields(String obj){
List<String> requiredFields = new List<String>();
Map<String,Schema.DescribeFieldResult> finalMap = getFieldMetaData(obj);
for(String fr: finalMap.keySet()){
if(!finalMap.get(fr).isNillable()){
requiredFields.add(fr);
}
}
return requiredFields;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment