# Setting Object Field Defaults

Field defaults allow organizations to ensure that data is entered correctly for new object records. Field defaults calculate and populate a given field with either a literal value or a formula-calculated value. Literal value defaults enter the exact value in the field.

Some example uses include:

  * Setting a default _Status_ or picklist value for an object record.
  * Creating default serial numbers by adding one (1) to a _Last Used Serial Number_ field value.
  * Creating a formula that evaluates a given field to set a default _Yes_ (expression is true) or _No_ (expression is false) on a field.
  * Setting a default field value <a href="/en/gr/32857/">specific to an object type</a>
.
  * Setting a default _User_ record for a <a href="/en/gr/28409/">_User_ object</a>
 reference field.

This functionality uses <a href="/en/gr/42857/">Vault's formula language</a>
 with object configuration.


<div class="note-border alert-info">
  <div class="alert alert-info" role="alert">
    <div><i class="far fa-info-circle"></i></div>
    <div class="alert-text">
      <p><strong>Note</strong>: Field defaults for <a href="/en/gr/15057/#object-reference-field">object reference fields</a>
 do not support the <code class="language-plaintext highlighter-rouge">RecordByLabel()</code> function.</p>
    </div>
  </div>
</div>



## Supported Field Types

Object field defaults are available for the following types of fields:

  * Number
  * Text
  * Yes/No
  * Date
  * DateTime
  * Picklist
  * Long Text
  * Rich Text
  * Object Reference (Set a default referenced object)
  * Parent Object (Set a default parent object)
  * Status (Set a default status)

The following field types are not supported:

  * Lookup
  * Name (Configure defaulting for the _Name_ field using <a href="/en/gr/30986/">System-Managed Object Record Names</a>
.)
  * Any system-managed field other than _Status_

## Order of Operations for Initial Object Field Values {#order-of-operations}

It's important to understand that other Vault features, such as Action Triggers and the _Update Field_ object record event action, can auto-populate the initial value of an object field. An initial field value refers to the field value displayed once the record is saved. These features can override a [field default value][2] during object record creation. In other words, field default values may not display as the field's initial value once the record is saved.

When you create an object record, initial field values are populated in the following order:

1. Fields appear on the Create Record page with [object field defaults][2] pre-populated.
2. Values a user manually enters on the Create Record page, and then clicks **Save**.
3. <a href="/en/gr/811001/#before-save">Before Save Action Triggers</a>
 configured in _Create_ record operations.
4. <a class="external-link " href="https://developer.veevavault.com/sdk/#Record_Triggers" target="_blank" rel="noopener">BEFORE_INSERT custom Vault Java SDK record triggers<i class="fa fa-external-link" aria-hidden="true"></i></a>.
5. Values generated by the <a href="/en/gr/51700/#object-record-action-types">_Update Field_</a>
 object record event action when it occurs in a _Create Record_ event.
6. If the field value is currently blank or null at this step before the [initial value and record is saved][4], Vault applies the object field default. This step ensures that fields configured with a default field value do not have a blank or null initial value.
7. Initial value saved and record is saved.
8. <a class="external-link " href="https://developer.veevavault.com/sdk/#Record_Triggers" target="_blank" rel="noopener">AFTER_INSERT custom Vault Java SDK record triggers<i class="fa fa-external-link" aria-hidden="true"></i></a>.
9. <a href="/en/gr/811001/#after-save">After Save Action Triggers</a>
 configured in Create record operations.

If your Vault has multiple features configured to auto-populate an initial field values, Vault creates the object record with the value that is populated last in this sequence.

For example, if you create a new _Product_ record where the _Country_ field has a field default value of _CAN,_ and the _Update Field_ object record event action is configured to set the field to _Canada_ on a _Create Record_ event, the _Country_ field's initial value becomes _Canada_.

### Blank Field Value Behavior {#blank-field-behavior}

Before the initial value and record are saved, the order of operations sets the initial field value if the field is blank. This means that if an object record field has a field default, that field can never accept a blank or null initial value. For example, if you create a new _Product_ record where the _Country_ field has a field default value of _Canada_, and you save the record with a blank value in the _Country_ field, the final step in the order of operations applies the field default as the _Country_ field's initial value. 

If you need to set a field with a default value to blank, you must do so after record creation. However, if the configured field default value is invalid, such as the value exceeding the field's character limit, Vault allows a blank or null value in the field when you save the record.

## Pre- & Post-Default Expressions

Depending on your default expression formula, Vault populates the default field value when a user creates (pre-default) or saves (post-default) an object record.

An expression is pre-default when it:

  * Only includes literal values
  * Only includes literal values or known field values based on an object or parent object reference relationship; this can only occur if the relationship is predetermined during the initial record creation. For example, when a child record is created from a parent record.

Any expression that is not pre-default is a post-default expression.

## How to Set a Field Default {#how-to-set-a-field-default}

To set a field default:

  1. Open an object from **Admin > Configuration > Objects**.
  2. Open the **Fields** tab and then click into a field.
  3. Click **Edit**.
  4. In the **Default Value** section, enter a literal value or use items from the **Fields**, **Functions**, and **Operators** tabs to create an expression. See [examples for default values][1], or learn more about functions and operators in the <a href="/en/gr/52324/">Vault Formula Reference Guide</a>
.
  5. Optional: Use the [**Records**][3] tab on object and parent object reference fields to build a formula that sets a single record as the default value. You can also click the {...} icon to select a field from an object relationship that references the same object.
  6. Click **Validate**. Vault will let you know if your expression syntax is valid.
  7. Click **Save**.

If your object uses object types, you can also <a href="/en/gr/32857/#type-defaults">set field defaults at the object type level</a>
. This overrides defaults set at the object level.

### Setting Field Defaults on Yes/No Fields {#default-examples}

When you configure a field default for a _Yes/No_-type object field, either with a formula or literal default value, enter `true` for _Yes_ and `false` for _No_.

For example, you may have a _Requires Yearly Review_ field (_Yes/No_) on your _Product_ object. You can configure a field default so that _Requires Yearly Review_ defaults to _Yes_ on all _Product_ records. Enter `true` in the **Default Value** field.

### Setting Field Defaults on Object & Parent Object Reference Fields {#setting-field-defaults-object-parent-object-reference}

When configuring a field default based on an object or parent object field, you can provide a single token that refers to another object reference field. The field that you point to may be on any related object. You can use the {...} icon in the formula editor to browse object relationships and select an object field that references the same object. Up to three (3) levels of outbound relationships are supported.

You can also use the <a href="/en/gr/52324/#recordbyuniquefield">`RecordByUniqueField()`</a>
 function to set any single record as the default field value. You can click the **Records** tab in the formula editor to use a dialog to build this formula instead of typing it manually. This function requires you to provide a unique field and record on the referenced object.

#### Example: Reference Object

You may want a newly created _Campaign Event_ record to have the same _Product_ relationship as its parent _Marketing Campaign_ record. To set this up, you'd provide the token that points to the _Product_ field on the related _Marketing Campaign_ record: `marketing_campaign__cr.product__c`

#### Example: Parent Object

In a CTMS Vault, you may want the _Study_ field (parent object) to default when you create a _Clinical User Task_ record from inside a _Monitoring Event_ record. Since _Monitoring Event_ also has a relationship to the _Study_ object, you can do so. To set this up, you'd use the following token as the default value: `monitoring_event__vr.study__v`

If the default value is invalid, such as an inactive record, Vault does not apply the pre-default expressions. Invalid default values in post-default expressions return an error. The `RecordByUniqueField()` function can filter out inactive records and does not apply the post-default value if no active record is found. If the defaulted field contains a dynamic reference constraint, `RecordByUniqueField()` can still return an error if a invalid record is returned.

### Setting Field Defaults on Picklist Fields

When configuring a default value for a picklist field, you can set a literal value or use the expression builder to set a value based on a formula.

<div class="note-border alert-info">
  <div class="alert alert-info" role="alert">
    <div><i class="far fa-info-circle"></i></div>
    <div class="alert-text">
      <p><strong>Note</strong>: Multi-value picklist fields can only use a literal value default.</p>
    </div>
  </div>
</div>



#### Example: Literal Value

If most employees are based in Ohio, you could set a literal value for the **Address - State** picklist field on the **Employee** object. For this, you would select the name value from the drop-down:

`Ohio`

#### Example: Calculated Value

If your **Employee** object includes an _Office Location_ field, you could dynamically default the _Address - State_ picklist field based on the _Office Location_ selection:

`If(office_location_c = 'dublin_ohc', Picklist.statec.ohio_c, null)`

### Setting Field Defaults on Rich Text Fields

When configuring a default value for a Rich Text field, you must wrap the entire value in double quotes. You can apply text formatting using supported HTML tags and attributes. Download the list of supported HTML tags and attributes <a class="download-link " href="https://platform.veevavault.help/assets/downloads/VeevaVaultRichTextSupportedHTML.zip" target="_blank" rel="noopener">here<i class="fa fa-download" aria-hidden="true"></i></a>.

For example, if you want the text to appear to users as red with the word "Canada" in bold, you would enter the following value:

`"<p style='color:#FF0000'>This site is located in <b>Canada</b>.</p>"`

Do not include <body> or <head> tags. Adding these tags to a Rich Text default value will have unexpected results.

Field defaults on Rich Text fields cannot exceed 3,900 characters, including HTML markup and variable names. If you need more characters, consider adding your content to a field and calling that field in your default value.

### Setting Field Defaults on Number Fields

If a default value is applied to a Number field, the field displays the full decimal places allowed, including trailing zeroes, when viewed on the record detail page.

For example, a Number field set to 5 decimal places with a default value of 100.123 displays 100.12300 as the default value. In addition, if the same Number field's default value is set to 100.123456789, the default value displays as 100.12345. Users can enter a different value in the Number field and the exact value is saved when the record is saved.

## Using Formulas for Field Defaults

When creating a formula, you can either type a formula expression into the **Default Value** field, or use items from the **Fields**, **Functions**, and **Operators** lists to create the expression. You can use the **Records** tab on parent object and object reference fields to create a default expression that uses a unique field and record on the referenced object. To move an item from these panels to the formula field, double-click on it or click the **Arrow** button. The formula field operates like a basic text editor.

Formula expressions referencing fields via relationships only support object relationships. For details about formula syntax and general guidelines for formulas, see <a href="/en/gr/42857/">Creating Formulas in Vault</a>
.

### Data Type

Your formula must return the type of data required for the field type. For example, a default formula for a text-type field must return text and a number-type field must return a number. If your formula returns the wrong data type, validation will fail and you will not be able to save.

### Blank Value Handling

If the evaluated expression results in null or blank, Vault does not populate the default value.

### Example Formulas

`last_used_serial_number__v + 1`

This formula adds one (1) to the value in the _Last Used Serial Number_ field. This default value would be useful for incrementing serial numbers for a _Product_ object.

`audit_start__c + Days(7)`

This formula defaults the _Audit End Date_ field as seven (7) days after the _Audit Start Date_ field. This default value can save users time and ensure consistency during audit planning.

### Default Single Value Reference Fields

Vault will automatically populate Object- or Parent Object- type fields if:

  * The field is empty.
  * The field is required.
  * The field references an object where only a single record is available for selection, either because only a single record exists or because of reference constraints.

This defaulting occurs during record creation and when editing an existing record.

 [1]: #default-examples
 [2]: #how-to-set-a-field-default
 [3]: #setting-field-defaults-object-parent-object-reference
 [4]: #blank-field-behavior
