For my case, I don't really need to know which required field is missing data, as long one of them is I want to thrown in a generic message.

Is there a method in apex that will tell me that one of the required field in SF is missing so that I wouldn't have to hard-code and check on all of the fields individually?

For example this is standard message for when a required field is missing: enter image description here


There is no generic method but you could create your own without having to hardcode and check the fields individually. For this, you need to do a describe on the object and include that in your controller or in a (before or after insert/update) trigger:

   List<String> requiredFields = new List<String>();    
   Schema.DescribeSObjectResult r = User.sObjectType.getDescribe();
    Map<String,Schema.SObjectField> M = r.fields.getMap();
    for(String fieldName : M.keySet())
        Schema.SObjectField field = M.get(fieldName);
        Schema.DescribeFieldResult F = field.getDescribe();
       // if F.isNillable() is false then field is mandatory
      if( F.isNillable())
         requiredFields.add(fieldName) ;

   for(YourObject__c yo : listOfYourObjectRecordsToCheck){
      for(String s : requiredFields){
         if(yo.get(s) == null){
            s.addError('A required field is missing'); // or any other way of raising an error
            // we can immediately continue to the next record

You can examine the Database.SaveResult records from using Database.insert/update/upsert(records, false), calling getErrors() to get a list of errors, then calling getStatusCode() on each error until you find a StatusCode.REQUIRED_FIELD_MISSING status code. A sample function that might check for such an error might look like:

boolean hasRequiredError(Database.SaveResult[] results) {
    for(Database.SaveResult result: results) {
        if(result.isSuccess()) continue;
        for(Database.Error error: result.getErrors())
            if(error.getStatusCode() == StatusCode.REQUIRED_FIELD_MISSING) return true;
    return false;

Note that this doesn't include custom validation rules, in which case you'll need to also check for FIELD_CUSTOM_VALIDATION_EXCEPTION and handle appropriately.

  • What if I am not doing a save? In my case, I have a commandbutton when click that calls some actionRegion and actionFunction components.
    – MCHam
    Oct 16 '14 at 15:49
  • You mean to say, "required fields" in Visualforce. You can't catch these validations, as they occur before your controller has a chance to run.
    – sfdcfox
    Oct 16 '14 at 15:53
  • Yes, What about catching the "VF_PAGE_MESSAGE" event that is log in the developer console, because that seems to run sometime after the controller runs?
    – MCHam
    Oct 16 '14 at 16:03

@MCHam: As I understand you want to identify required field values (marked as required in visualforce) within VF page itself, as soon as user clicks commandbutton.

You can add an "onclick" javascript handler on commandbutton. It can invoke a javascript method which can validate data for required fields and display an error message if any required field is missing value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.