function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
sdbsdb 

Determining if a field is in error using reflection on an sObject

I've seen in the doc that it is possible to add an error to a specific sObject field using dot notation. Ie 

 

Account myAccount  = [select an account];

myAccount.phone.addError("Invalid phone number");

 

It also appears to be possible to get a field's value by passing the field name as a string. 

 

In both cases you need to know the specific sObject and field name ahead of time. What I would like to do is write a generic method that iterates over the list of fields associated with any sObject, checking to see if a field is in error.

 

In pseudo code this would look something like this:

 

public Boolean recordHasFieldsInError(sObject s) {

Boolean inError = false;

List<Field> fields = sObject.getFields();

for(Field f: fields) {

if(f.hasError()) {

inError = true;

break;

}

}

return inError;

}

 

Is there a way to do this in Apex without knowing the sObject type or the names of the fields ahead of time?