Unique, Non Required Field Oddity

Salesforce provides the ability to mark fields as required and as unique. These options work exactly as you would expect them to work. If you try to submit a record with an empty required field, you get an error.

If you try to submit a unique field with a duplicate value, you get an error.

So, none of this is really a surprise. This is how Salesforce explains these features will work. Typically, this functionality is used together to enforce a user to enter a value into a unique field. However, there may be a time where we want just a single instance of the object with the unique field set to null. If the field isn’t required, we should be able to have a single instance of null and then any duplicates will error out. Considering Salesforce describes the unique feature as “preventing duplicate field values”, it is reasonable to expect a second null instance to error out. However, that is wrong!

While in every other instance, null == null or '' == '' would return true, in the scenario where you have a field marked as unique but not did mark it as required, you can save multiple instances of that object without ever specifying the unique field. Like below:

With my unique field set up as:

I can progress through the following screen twice by just pressing Save:

This results in:

As you can see, a unique field is only considered unique if you enter a value. Two empty strings do not fire off the validation. This can be a problem and something that should be considered when creating a truly unique field.

One Response to “Unique, Non Required Field Oddity”

  1. Ryan Beller
    October 8, 2014 at 8:45 pm #

    There’s a good deal of misinformation out there about this topic. Glad someone got it right and included proof!

Leave a Comment