5

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){
                mandatoryFields.add(fieldName);
            }
        }
        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>();
        accountFieldList.addAll(accountFieldSet);
        for (Account a : updateAccounts){
            for (String fieldName : accountFieldList){
                if (mandatoryFields.contains(fieldName)){
                    if (a.get(fieldName) == null){
                        accountsToUpdate.add(a);
                    }
                }
            }           
        }
        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.

2
  • 1
    What about fields that are made Required on the Account Page Layout? Is it okay that you're not including those also? Jan 10, 2014 at 17:45
  • Hi Jeremy, thanks for the reply. I'm not sure if I understood you correctly. All the mandatory fields are in accountFieldSet, including Account Page Layout. But I want to see only those mandatory fields which exist on the layout where the account was created
    – justasd
    Jan 10, 2014 at 21:50

1 Answer 1

3

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:

<layoutColumns>
    <layoutItems>
        <behavior>Edit</behavior>
        <field>OwnerId</field>
    </layoutItems>
    <layoutItems>
        <behavior>Required</behavior><!-- we have a winner! -->
        <field>Name</field>
    </layoutItems>
    <layoutItems>
        <behavior>Edit</behavior>
         <field>ParentId</field>
    </layoutItems>

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. ???
  7. PROFIT

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .