Apex: check if a record has empty required fields

How to check if a specific Account record has mandatory fields that are empty? What could be the steps of doing it?

This is what I was trying to do (but with no luck):

    //All field names of the Account object
    static Set<String> accountFieldSet = Schema.SObjectType.Account.fields.getMap().keySet();

    //Map of Account fields
    static Map<String, Schema.SObjectField> accountFieldMap = Schema.SObjectType.Account.fields.getMap();

Here’s the method for getting all the mandatory fields:

public static Set<String> getMandatoryFields(){
        Set<String> mandatoryFields = new Set<String>();
        for(String fieldName : accountFieldSet){ 
            Schema.SObjectField field = accountFieldMap.get(fieldName);
            Schema.DescribeFieldResult describeFieldResult = field.getDescribe();
            Boolean isFieldMandatory  = !describeFieldResult.isNillable();
            if (isFieldMandatory){
        return mandatoryFields;

Here’s the method for getting all the Account records that have empty mandatory fields:

public static List<Account> getAccountsToUpdate(List<Account> updateAccounts, Set<String> mandatoryFields){
        List<Account> accountsToUpdate = new List<Account>();
        List<String> accountFieldList = new List<String>();
        for (Account a : updateAccounts){
            for (String fieldName : accountFieldList){
                if (mandatoryFields.contains(fieldName)){
                    if (a.get(fieldName) == null){
        return accountsToUpdate;

And here’s the the code execution portion:

    try {
        update updateAccounts;
        } catch (System.DmlException ex) {
            if (ex.getDmlType(0) == StatusCode.REQUIRED_FIELD_MISSING){
                Set<String> mandatoryFields = AccountBILevelCalc.getMandatoryFields();
                AccountBILevelCalc.getAccountsToUpdate(updateAccounts, mandatoryFields);
                //not finished

The problem that I face is that accountFieldSet also holds the fields that don’t exist on the form from which the account was created. That’s why the list of mandatory fields also holds the fields that I don’t care about.


You would need a “describe layout” call to learn which fields were marked mandatory (either because they’re required on the database level or because somebody marked them as such on the layout). The piece of XML that looks promising is similar to this:

        <behavior>Required</behavior><!-- we have a winner! -->

Check https://stackoverflow.com/questions/14607749/how-to-retrieve-salesforce-page-layout-field-properties-via-api – it’s something but for you the extra hoop is that Apex doesn’t have the describeLayout call. It’s available in Metadata API (used by external tools like Force.com IDE).

Sadly that answer I’ve linked is still not outdated…

  1. There are some describe layout-like calls now but they seem to be related to Actions (buttons + small page layouts, looks very Chatter-y)
  2. Tooling API is promising… but for now it doesn’t include page layouts.

If you’re still keen to fetch the page layout & parse it – check the MD API for Apex developed by Andrew Fawcett.

If you’re discouraged (I’d be) – do it manually.

  1. Fetch the page layouts to Eclipse/Force.com IDE or just visually scan them in the layout editor.
  2. Write down list of required fields per page layout.
  3. Confront it with the recordtypes (in the page layout assignment window)
  4. Write queries / reports that search for records with recordtype X and fields Y and Z missing.
  5. Bonus points for adding validation rules to the equation? Triggers?
  6. ???

Source : Link , Question Author : justasd , Answer Author : Community

Leave a Comment