Skip to content

Instantly share code, notes, and snippets.

@jason-den
Last active July 8, 2021 23:26
Show Gist options
  • Save jason-den/6e15fb56eda401a65f4c095a532d100d to your computer and use it in GitHub Desktop.
Save jason-den/6e15fb56eda401a65f4c095a532d100d to your computer and use it in GitHub Desktop.
Get required fields from a given object. Code is supposed to execute as anonymous code in SF developer console
string objectName = 'Assessment__c'; // Usage: change the object name here
Map<String, Schema.SObjectType> gdesc = Schema.getGlobalDescribe();
Sobject objectt = gdesc.get(objectName).newSObject();
Schema.DescribeSObjectResult r = objectt.getSObjectType().getDescribe();
Map<String,Schema.SObjectField> M = r.fields.getMap();
for(String fieldName : M.keySet()) {
Schema.SObjectField field = M.get(fieldName);
Schema.DescribeFieldResult F = field.getDescribe();
Boolean isFieldreq = F.isCreateable() && !F.isNillable() && !F.isDefaultedOnCreate();
if (isFieldreq) {
System.debug(fieldName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment