Making Fields Required in Salesforce

Making Fields Required in Salesforce

In this blog, I share some important points while dealing with required Salesforce object fields.

·

5 min read

Common Confusion

Many a times we salesforce developers find ourselves in critical situations while facing errors due to required salesforce object fields. We get confused between required standard fields and required custom fields at times. Sometimes, the errors themselves are tricky to understand and debugging consequently eat lot of our precious time. Hence, we all would love to have something handy to look upon when needed.

Common Questions in our minds

  • What happens if we don't give required fields while inserting data through insert or Database.insert ?
  • What happens if we don't give required fields while inserting data through REST/SOAP API ?
  • Why I don't see standard fields having certain options available such as Required?
  • How many ways are there to make a field required in salesforce?
  • Will Apex give an error if the field is required at page layout level?

Important Points to Remember

There are basically three levels to make a field required :

1. At schema level(or field level)
        This is the most restrictive of requirements, and it requires the field to be entered all the   
        time, regardless of how the record is saved (i.e. through apex, an integration, the API, 
        mass upload, or through the User Interface).
2. At Validation Rule level
        ISBLANK(Enter_Custom_Field_API_Name_Here__c)
        Or, if the field is a Number or Currency type field use this syntax: 
        ISNULL(Enter_Custom_Field_API_Name_Here__c)
        Like the field level setting, this will apply all the time, regardless of where the record is 
        created or updated.
3. At the Page Layout level
        This requirement only applies when the record is edited on the user interface.
        Therefore, you could technically make the field required for some users that use a 
        particular page layout but not others.            
        But, when records are updated via API, field requirement does not apply. 

Here, I have covered the required field in three types :

Always Required Standard Fields :

Example : Name field on Account object, LastName field on Contact object.

  1. If we don't give always required standard fields while inserting data through insert or Database.insert, we encounter an error saying :

      System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing : [fieldName]
    
  2. If we don't give always required standard fields while inserting data through REST API or SOAP API, we encounter an error saying :

      message: Required fields are missing: [fieldName]
      errorCode: REQUIRED_FIELD_MISSING
    

Required Standard Fields :

Standard fields don't have certain options available such as Required. This is not an issue and is working as designed by the salesforce. Thus, standard fields which are not required by default are made required either through validation rules or page layout.

Example : Phone field on Account.

If a field is marked required in a Validation Rule (Setup -> Object Manager -> Account -> Validation Rules (Valdiation Rule for Field), then :

  1. If we don't give required standard fields while inserting data through insert or Database.insert, we encounter an error saying :

          System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Error Message
    
  2. If we don't give required standard fields while inserting data through REST API or SOAP API, we encounter an error saying :

          message: validation rule error message
          errorCode: FIELD_CUSTOM_VALIDATION_EXCEPTION
    

If a field is marked required in a Page Layout ONLY (Setup -> Object Manager -> Account -> Page Layouts (Mark Field as Required in Layout Editor), then :

If we don't give required standard fields while inserting data through insert, Database.insert or through REST API, SOAP API, we don't encounter any error and record gets inserted successfully.

Required Custom Fields :

Custom fields which are made required either through schema level or validation rules or page layout.

Example : sample__c field on Account object.

If a custom field is marked required in the Schema (Setup -> Object Manager -> Account -> Fields -> Click Custom Field (Mark true: Always require a value in this field in order to save a record), then :

  1. If we don't give required custom fields while inserting data through insert or Database.insert, we encounter an error saying :

          System.DmlException: Insert failed. First exception on row 0; first error: REQUIRED_FIELD_MISSING, Required fields are missing : [fieldName]
    
  2. If we don't give required custom fields while inserting data through REST API or SOAP API, we encounter an error saying :

          message: Required fields are missing: [fieldName]
          errorCode: REQUIRED_FIELD_MISSING
    

If a custom field is marked required in a Validation Rule (Setup -> Object Manager -> Account -> Validation Rules (Valdiation Rule for Field), then :

  1. If we don't give required custom fields while inserting data through insert or Database.insert, we encounter an error saying :

          System.DmlException: Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Error Message
    
  2. If we don't give required custom fields while inserting data through REST API or SOAP API, we encounter an error saying :

          message: validation rule error message
          errorCode: FIELD_CUSTOM_VALIDATION_EXCEPTION
    

If a field is marked required in a Page Layout ONLY (Setup -> Object Manager -> Account -> Page Layouts (Mark Field as Required in Layout Editor), then :

If we don't give required custom fields while inserting data through insert, Database.insert or through REST API, SOAP API , we don't encounter any error and record gets inserted.

Conclusion

Salesforce has already made few fields mandatory which are required by default and we need to enter values in such fields in order to save the record. I have defined such fields as Always Required Standard Fields. There are other standard fields too which salesforce has not made required by default. We as admin can make them required through page layout and validation rule as there is no option of Required checkbox at schema level (field level). But, we have an additional option to make any custom field required at the field level itself. So, we as developers must keep these key points in mind while creating data model and during debugging stage.