I need to take all mandatory FIELD NAMEs (not FIELD LABEL) so that I can create object. What are the ways to do it outside copying each FIELD NAME from Object Manager?
1 Answer
Open the Developer Console window and run this snippet:
Map<String, Schema.SObjectField> fieldMap = Product2.sObjectType.getDescribe().fields.getMap();
Set<String> sysNAuditFields = new Set<String> {'Id',
'IsActive',
'IsDeleted',
'IsArchived',
'CreatedDate',
'CreatedById',
'LastModifiedDate',
'LastModifiedById',
'SystemModstamp'};
for (Schema.SObjectField f : fieldMap.values()) {
Schema.DescribeFieldResult fieldDesc = f.getDescribe();
if(!fieldDesc.isNillable() && !sysNAuditFields.contains(fieldDesc.getName())) {
system.debug(fieldDesc.getName());
}
}
Change Product2 for the API name of whichever object you need mandatory fields for.
Note: This will give all mandatory fields at an object level, not those that may have been tagged as required on a specific page layout for the object.
Sample log result for this query in my org