Skip to content

Instantly share code, notes, and snippets.

@b-wilder
Last active August 29, 2015 14:17
Show Gist options
  • Save b-wilder/0cd489bd8f1025be6068 to your computer and use it in GitHub Desktop.
Save b-wilder/0cd489bd8f1025be6068 to your computer and use it in GitHub Desktop.
Get all creatable fields for any SObject (Apex, Salesforce)
public static set<string> getCreatableFields(Schema.SObjectType objectType)
{
Map<String, Schema.SObjectField> fMap = objectType.getDescribe().Fields.getMap();
set<string> creatableFields = new set<string>();
if (fMap != null){
for (Schema.SObjectField ft : fMap.values()){ // loop through all field tokens (ft)
Schema.DescribeFieldResult fd = ft.getDescribe(); // describe each field (fd)
if (fd.isUpdateable()&&!fd.isUnique()){ // field is creatable
creatableFields.add(fd.getName());
}
}
}
return creatableFields;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment