Setting default values at the time a new document (file, picture, video, custom document type, etc.) is created is a common requirement. It can be a strict business rule or just a suggestion for the end user. In the Nuxeo Platform, there are basically three different places where you can set default values:

  • In the definition of the schema
  • In a widget
  • In an event handler

Thanks to Nuxeo Studio, you can handle all these cases pretty fast - mainly the clicks and drag and drops because the main goal of Nuxeo Studio is to let you configure your business applications in a cool and easy way. That’s why I say “Nuxeo Studio Rocks!”.

Using a Schema

Let’s start with the fastest and easiest way to define a default value - setting it when you define a schema. These values are set as soon as a new instance of the document is created, before it is stored. For example, in this screenshot we have the Creation layout displayed (on the right) to the user, who still have to click the “Save” button to actually store the document. The user can modify the values before clicking the Save button. If there are any default values that shouldn’t be modified you can just hide the field or set the widget to read-only:

Creation layout

These default values are used irrespective of the way the document is created. The values would have been exactly the same if the document was created from a REST call for example. When setting default values in the schema, they can only be hard coded and can’t be dynamically calculated (for example, setting a date field to the current date).

Using a Widget

In this case, you are creating a layout in Studio - the Creation layout, and you want to set to a default value for a field. As of today (February 16, 2016), the Layout Widget Editor doesn’t display any property that will let you set a default value. But, this can be done another way. Most widgets (text, numeric, etc.) accept a defaultValue property. So here’s what you can do:

Scroll down to the “Custom Properties Configuration” part of the editor and add this defaultValue property with the value you need. Here, we are setting the value of the claim:department field to “Marketing”:

Layout Widget Editor

Using this feature, just like for default values set in a schema, the value is applied before the document is actually stored and the user can modify it. With this default-value-in-widget approach, you can have different default values for different Document Types using the same schema for instance.

So far, the example in the previous screenshot shows a hard coded value, just like for default values set in the definition of a schema. But if you want a dynamic value, you can put an EL in this defaultValue property. This opens a lot of cool possibilities!

For example, you could use #{currentDate} to set a date widget to “today”. You can even use the ternary operator to set a value depending on a condition. For example, our integer field is set to 3 or 10 depending on the fact that the user is in the MainUsers group or not:

#{currentUser.isMemberOf('MainUsers') ? 3 : 10}

Notice that in the case of a default value set in a widget, since it is a pure UI element, the value will not be applied if the document is created via REST or using any other client.

This is where you can use an Event Handler.

The Empty Document Created Event

This event is triggered when a new document is instanciated, before it is saved and stored for good. Here you can set the values using the Document > Update Property or Document > Update Properties operations, either from an Automation Chain of from a JavaScript chain. Just keep in mind not to save the document because as it does not yet really exist in the database and you will get some errors in the log (cannot save document 'null' with null reference: document has probably not yet been created in the repository).

Event Handler

For example, in the chain called by the Event Handler you can set a date to 5 or 10 days in the future depending on whether the user is in the MainUsers group or not:

@{CurrentUser.allGroups.contains("MainUsers") ? CurrentDate.days(5).calendar : CurrentDate.days(20).calendar}

Or you could set a custom reference using a sequence number (something like “2016-02-785”):

@{CurrentDate.format("yyyy-MM")}[email protected]{Fn.getNextId("CLAIM")}

Here, just like for a default value in a schema, the value is set irrespective of the way the document is created.

The About to Create Event

This event occurs just before the document is saved in the database(1). So it is a bit different from all the previous ones because at this step the user has no way to change the value. For example, maybe you prefer to set a custom reference based on sequence number here, so the sequence number is not incremented if, in the UI, the user cancels the creation.

Also, this event is a perfect place to set up fields whose value is based on other fields (string concatenation, operations on numbers, etc.),

All Together

Maybe you want to set-up your default values everywhere. Maybe you have complex conditions and sub-conditions, for example, you let the default value set in schema in some cases but change it in other cases.

Here is the order in which default values are applied:

  • First, the default value set in the defaultValue property of Widget
  • Then, the one set in a schema
  • Then, the one set in the Empty Document Created event
  • Then, the one set in the About to Create

Let’s say you have a numeric field set to 1 in the defaultValue property of a widget, 2 in the schema definition, 3 in Empty Document Created, and 4 in About to Create. Now, back to school for a small quiz: If the user displays the “Creation” layout in the UI, sets the value of the field to 10 and save, what will be the final value stored?(2).

Other Ways of Doing It

Well. This would actually go beyond Studio. If you have very specific needs you could write a Java plugin pre-filling values depending on very complicated conditions or context. And this is what makes the Nuxeo platform so cool: It is an extensible platform.

These were at least four different ways to handle default values in just seconds. Having choices and making choices quickly is important in life: The Nuxeo Platform and Nuxeo Studio give you the choices. That’s why they rock!

(1) Whatever the database. Remember how flexible the Nuxeo platform is: You can use PostgreSQL, Oracle, MS SQL Server, MySQL, MongoDB and in a near future, CouchBase (2) I will let you answer this! After all I’m sure you are sure about your answer.