With AODocs, you can set up conditional logic in document classes in any type of AODocs library. When users edit their documents, conditional logic is applied as follows:
- property values can be calculated automatically
- properties can be conditionally mandatory or hidden
- document titles can be calculated automatically (in Document Management libraries only)
Conditional logic is calculated using formulas that can include references to other entities such as custom or system properties, custom property states (mandatory or hidden) and the document title.
Before you use this article, you should:
- be familiar with the basics – learn more: What is conditional logic in AODocs?
- know how to set up conditional logic in the library configuration to:
- be at ease with building simple formulas with copied code snippets – learn more: Getting started with formulas for conditional logic
- have experience adding entity references to your formulas and playing with entity references in JavaScript
- be familiar with the JavaScript programming language, conforming to the ECMAScript® 2020 specification
This article explains the JavaScript syntax you can use for different types of conditional logic. It also provides examples of the corresponding calculated values you can expect.
Use this article alongside the article Create your own formulas: play with entity references in JavaScript to learn how to be creative with conditional logic according to your specific business needs.
Automatically generated table of contents
Note about different types of JavaScript syntax
In the examples we give in our articles about conditional logic (learn more: Getting started with formulas for conditional logic), we use two types of JavaScript syntax:
- single expression
- multiple instructions, wrapped in an arrow function
You can use either in your formulas, but we recommended taking account of the following considerations.
General rule: the JavaScript code defining a formula must be made of one, and only one, expression.
This means that:
- if your logic can be expressed in a single expression, you can type it directly in the formula builder, for example:
propertyString`Project name` + ' - ' + sequenceId`document`
- if your code requires additional instructions (such as when processing dates), you should wrap all your statements in an arrow function definition
(() => { ... })
, and call it right after the definition with an arrow function call()
:
(() => {
const d=new Date(propertyDate`Publication date`)
const yyyy=d.getFullYear()
const mm=`${d.getMonth()+1}`.
const dd=`${d.getDate()}`.padStart(
return `${mm}/${dd}/${yyyy}`
})()
Tip: To insert a multiline formula structure, start typing multi
in the formula builder and select multiline
.
However, you can play with JavaScript to combine these multiple instructions into a single expression on a single line:
`${`${(new Date(propertyDate`Publication date`))
.getMonth()+1}`.padStart(2,'0')
}/${`${(new Date(propertyDate`Publication date`))
.getDate()}`.padStart(
You can then enter this directly in the formula builder without using the arrow function definition.
We recommend using the arrow function definition because with the single-line expression:
- there's a lot of repetition, which is not good practice; in our example
new Date(propertyDate`Publication date`)
is repeated three times - the expression is long, which makes it difficult to read and debug
But it's your choice!
Calculated values for String or Text properties and calculated titles
String and Text properties, and also calculated titles, accept as input:
- Strings
- Integers
- Decimals
- Booleans
They also support simple math calculations, which are evaluated when the formula is calculated and the result of which is displayed. Learn more: Use simple math calculations.
Below are some supported JavaScript expressions with their expected values.
JavaScript expression |
JavaScript type |
Calculated property value or title |
Comment |
'Document validated' |
String |
Value: |
Single quotes |
"Document validated" |
String |
Value: |
Double quotes |
1234 |
Number |
Value: |
No formatting |
3.0 |
Number |
Value: |
No formatting |
1+2 |
Number |
Value: |
Evaluation first |
|
Boolean |
Value: |
English |
`${3+4} documents` |
String |
Value: |
String literal |
Calculated values for Integer properties
The nominal expected value is a JavaScript Number, but a best effort is made to also map String types.
Below are some supported JavaScript expressions with their expected integer values.
JavaScript expression |
JavaScript type |
Calculated property value |
Comment |
|
Number |
Value: |
Nominal integer |
|
Number |
Value: |
Operation |
|
Number |
Value: |
No decimal part |
|
Number |
Value: |
Round-down |
|
Number |
Value: |
Round-down |
|
String |
Value: |
String parsing integer |
|
String |
Value: |
String parsing decimal |
Calculated values for Decimal properties
The nominal expected value is a JavaScript Number, but a best effort is made to also map String types.
Below are some supported JavaScript expressions with their expected decimal values.
JavaScript expression |
JavaScript type |
Calculated property value |
Comment |
3.14 |
Number |
Value: |
Nominal decimal |
1.2+3.8 |
Number |
Value: 5 |
Operation |
4 |
Number |
Value: 4 |
Integer |
2 + 3.1 |
Number |
Value: |
Hybrid operation |
‘6.626’ |
String |
Value: |
String parsing decimal |
1 + 0.61 |
Number |
Value: |
Double precision: |
Calculated values for Boolean properties and conditionally mandatory or hidden properties
For Boolean properties and conditionally mandatory or hidden properties, the nominal expected value is a JavaScript Boolean, but a best effort is made to also map String types.
Below are some supported JavaScript expressions with their expected boolean values.
JavaScript expression |
JavaScript type |
Calculated property value or conditionally mandatory/hidden property |
Comment |
true or false |
Boolean |
Value: |
Nominal |
‘true’ or ‘false’ |
String |
Value: |
String in english |
‘TRUE’ or ‘FALSE’ |
String |
Value: |
String in ENGLISH |
Calculated values for Date, Date&Time or Time properties
The nominal expected values are either JavaScript Date or Number. Strings are not supported.
Notes:
– Dates and times are given in Epoch milliseconds (the number of milliseconds since January 1, 1970, 00:00:00 UTC).
– Months in JavaScript start at zero: January is 0, February is 1, and so on. Learn more: Extract the month.
– Date properties must use a value that corresponds to midnight UTC, otherwise an error occurs.
– The display of dates varies according to the user's locale. For example, en_GB, en_US and fr_FR all display dates differently.
Below are some supported JavaScript expressions with their expected Date values.
JavaScript expression |
JavaScript type |
Calculated property value |
Comment |
Date |
AODocs Date property: |
JavaScript Date |
|
|
Number |
AODocs Date&Time property: Value: |
AODocs translates the number into a JavaScript Date |
|
Number |
AODocs Time property: Value: |
AODocs translates the number into a JavaScript Date |
|
Date |
AODocs Date&Time property: Value: Note: If you don't indicate time in the code, the time defaults to 12 AM UTC (or 12 AM UTC plus however many hours ahead of UTC your timezone is). |
JavaScript Date |
|
Date |
AODocs Date&Time property: Value: |
JavaScript Date |
Calculated values for Category properties
The nominal expected value is a String equal to the selected category value ID.
JavaScript expression |
Structure of the category value entity reference: For example:
Important: Use the code injector in the formula builder. Learn more: What is the formula builder?. |
JavaScript type |
String |
Value of calculated Category property |
Value: |
Calculated values for Person properties
The nominal expected value is a String equal to the email address of the person or group you are referring to.
JavaScript expression |
Email address. For example: alice@demokb.aodocs.com |
JavaScript type |
String |
Value of calculated Person property |
Value: |
Note: For a Person property to accept groups you must set it as multivalue.
Calculated values for URL properties
The nominal expected value is a String. In its simplest form, this value is equal to the web address. It's also possible to provide a label that is displayed instead of the address. You can use the syntax: [label](address)
.
Below are some supported JavaScript expressions with their expected URL values.
JavaScript expression |
JavaScript type |
Value of calculated property |
Comment |
"https://www.aodocs.com" |
String |
AODocs simple URL property: |
The raw address is displayed in the AODocs document |
"[AODocs website](https://www.aodocs.com)" |
String |
AODocs URL property with label: |
The label is displayed in the AODocs document |