I have a list of field api names of type string belong to particular object. I want to validate whether the field has value or not at the time of saving the record and throw error message exactly at the field location if the value is not present for the field.

        Integer count=0;
        Map<String, Schema.SObjectField> M =Schema.SObjectType.Account.fields.getMap();
  String fieldApiName;
        for(String s  : psProductFields)
           fieldApiName = s;

            if(AccountObj.get(s)==null || AccountObj.get(s)=='')

                 AccountObj.fieldApiname.addError('please provide the value');

1 Answer 1


If you are looking for adding an error message to the field dynamically, then please go through Provide an additional addError method on SObject that accepts a field name, it seems to be an idea.

The Developer Guide says, This method is highly specialized because the field identifier is not actually the invoking object—the sObject record is the invoker. The field is simply used to identify the field that should be used to display the error.

You must log in to answer this question.

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