How to Write a Formula

Summary

Many parameters for “Do” and “Conditions” can include formulas.

Using these formulas makes it possible to reference customized field values and the results of calculations from other actions.

Locations Where Formulas Can Be Applied

Formulas can be used when entering parameters, and the following types of parameters will be applicable:

Locations Where Formulas Can Not Be Applied

  • Parameters limited to field selection

  • Parameters that refer to the results of other actions

  • Do “Run JavaScript

Two Types of Writing Formulas

There are two primary methods for writing formulas:

=expression

Beginning a parameter with “=” indicates that all subsequent text is considered a formula in text-type parameters. This is similar to entering a formula in an Excel cell.

= 1 + 2

The field mapping describes the formulas in this format for each field in each set. For details, see “How to describe field mapping”.

${expression}

The “=formula” expression cannot be used in the Text area or Rich text fields. Instead, place expressions within ${}. The text outside of ${} will remain as regular wording.

The result of 1 + 2 will be ${1 + 2}

You can also use ${expression} in text-type parameters. However, you cannot mix = expression and ${expression}.

Available Description Methods

Parameter Type Available Expression Formats
Text =expression, ${expression}
Text area ${expression}
Rich text ${expression}
Field Mappings =expression
Text template ${expression}

How to Reference a Field Value

You can obtain the value of a field by incorporating a field code into your formula. In the example below, you can calculate the ratio of the fields “number_1” and “number_2”:

= 100 * number_1 / number_2

“Which record to refer to” depends on the context. It refers to the “record currently being displayed or edited on the page”, but in field mapping, it may also refer to the source record for mapping.

To explicitly refer to the “current page’s record” regardless of the context, use “$$”. In the following example, “number_1” is explicitly specified to refer to “the record of the current page,” while “number_2” is context-dependent.

= 100 * $$.number_1 / number_2

Referring to More Detailed Attribute Values (*For Advanced Users *)

Some fields have more detailed attributes, such as “Login Name and Display Name” in “User Selection.” You can access the attributes.Use a dot (”.") to refer to them. The following example refers to the login name of the record creator. For information on which field types have which attributes, refer to the Kintone official documentation .

= creator.code

Referring to the Results of Other Actions

Writing something like $1 in the expression is treated as the action number, and the result value of the action is obtained.

= $12 + $13

The notation “$number” in ${expression} format is illustrated in the following example:

Correct: The result of action number 12 is ${$12}.

In the following example, the segment $12 does not convert into an expression, which prevents it from producing the expected result.

Incorrect: The result of action number 12 is $12.

Text

When dealing with fixed strings in an expression, enclose them in double-quotes.

To include a line break in a string, you can write “\n” instead of a character, and that part will become a line break. The following example creates a string that separates “Tokyo, Nagoya, Osaka” with line breaks.

= "Tokyo\nNagoya\nOsaka"

When concatenating multiple strings, use “&” to concatenate. Please note that it is not “+”.

= "Today's date is"  & date_1 & "."

Number

Basic arithmetic operations (+-/*) and modulus (%) can be used for numerical values.

Multiplication is denoted by the half-width space “* " instead of “×”.

Division is indicated by a half-width “/” instead of “÷”.

Array (*For Advanced Users *)

There are items in the fields that can have multiple values, such as Check boxes and User selections. Such fields hold values in a format called “array.”

You can access each element of the array using “[]” just like in JavaScript. The following example retrieves the first value selected in the check box field.

= checkbox[0]

The following example creates an array with two elements, “sample1” and “sample2”.

= ["sample1", "sample2"]

When attributes are specified for an array, the result will be an array that extracts the attribute values for each element of the original array. The following example returns the result represented as an array of the login names of all users selected in the user selection field.

= user_selection.code

Binary operators (the type of operation that has a left-hand side and a right-hand side) can have the left-hand side as an array. In this case, a new array is created by applying that operator to each element of the array on the left side. The following example returns an array of [5, 6, 7].

= [1, 2, 3] + 4

When the right-hand side is an array with a binary operator, it usually results in an error. However, if the array on the right side has only one element, that element will be extracted and operated on. The following example is interpreted as 6 + 4, and the result is 10.

= 6 + [4]

An error occurs when the right side has two or more elements. The following example will result in an error at runtime.

= 1 + [2, 3]

Using Operators and Functions in Expressions

Please refer to the following linked pages for more information:

Differences Between Kintone App Customization and Job Runner Expressions

In Kintone app customization (which features a green theme on the customization page) and Job Runner (which has an orange theme on the customization page), there are differences in behavior when a non-existent field is specified.

  • In Kintone app customization, if a non-existent field is included in the expression, it does not trigger an error; instead, the result will simply be empty.

  • Conversely, in Job Runner, specifying a non-existent field will lead to an error, and the job will be halted.