How to print all the mandatory fields of a Record in an object with the help of SOQL query?
You can’t use a query for this. Instead, you can use a describe call.
sObjectField[] mandatoryFields = new sObjectField[0];
for(sObjectField field: sObjectType.Account.fields.getMap().values()) {
if(!field.getDescribe().isNillable()) {
mandatoryFields.add(field);
}
}
However, keep in mind this won’t catch fields that are required by a page layout or validation rule. It finds only fields that the database deems required, and doesn’t report fields that are mutually required (e.g. one of two fields on the record must be populated).
Read more here: Source link
