How to Write a Text Template

Summary

This explains how to write templates used in “Generate Text from Template” (Kintone app customization version / Job Runner version).

The template is saved as a text file and set in the Attachment field or within the Text area field. When setting in the attachment field, please ensure the file’s character encoding is UTF-8.

This template is not in Excel format. Please create it in text format.

Elements Describable in the Template

You can write the following elements in the template:

  • ${expression}
  • $IF
  • $FOR

${expression}

You can write Customine expressions with ${expression}. This expression is evaluated at runtime and replaced with its value. You can also use the Customine function in the expression.

“=expression” cannot be used.

In this expression, you can refer to the “Data Record” parameter of “Generate Text from Template” (Kintone app customization version/[Job Runner version) using @. @ will be an array of records.

For example, if there is a field called “purchase date” in the data record:

Purchase date: ${format(@[0].purchase_date, 'YYYY year M month D day')} 

can get

Purchase date: May 14, 2024

as an output.

If you simply write${field}, that field will not be a field of the data record, but a field of the record on the page. To refer to the data record, please refer through @.

$IF

By using $IF, you can write blocks that will only be output if certain conditions are met.

$IF is described as follows:

$IF condition expression
Content
$END

In a conditional expression, describe an expression that can be represented as true or false. If it is not a boolean value, it will result in an error. **Please do not enclose this conditional expression in {} **.

If the evaluation result of the conditional expression becomes true at runtime, the content up to that point will be output. The content can be described in multiple lines, and ${expression} can also be used. If the condition is false, nothing will be output.

Dear ${name}
Thank you for your purchase.
$IF membership_type = 'Gold'
Special announcement for Gold members
Points will be increased by ${bonus}% until ● month ● day ,● year!
$END

The expression of Customine does not have the operators and/or. To combine conditions with and/or, use the And function or the Or function.

Also, there is no syntax like $ELSE that outputs when the condition of $IF is not met. It is necessary to describe the conditions one by one.

$IF amount < 1000
Point granting rate 3%
Expiration date 1 month
$END
$IF and(amount >= 1000, amount < 10000)
Point granting rate 5%
Expiration date 3 months
$END
$IF amount >= 10000
Point granting rate 8%
Expiration date 6 months
$END

You cannot write text before or after $IF and $END. If there is text before $IF, that line is considered not a $IF line and is treated as a normal text line. If there is text after $IF, that text is considered part of the conditional expression.

$FOR

You can repeatedly generate output based on the number of elements in the array.

$FOR is described as follows:

$FOR variable name IN expression
Content
$END

or

$FOR variable name, index variable name IN expression
Content
$END

In the “expression”, you will describe a Customine expression that results in an array. If it is not an array, it will result in an error. Please do not enclose this expression in {}.

It loops through each array element resulting from the expression, setting the variable name and outputting it until $END. If the result of the expression is an empty array, nothing will be output. The content can be described in multiple lines, and ${expression} can also be used. The variable name can be referenced by ${variable name}. If you specify the index variable name, you can refer to the index value with ${index variable name}. The index value counts from zero.

A typical pattern is to specify @ in the expression to output data records. @ represents an array of data records, so the “variable name” contains one line of the record.

Subject, Date
$FOR record IN @
${record.subject},${record.date}
$END

You cannot write text before or after $FOR and $END. If there is text before $FOR, that line is considered not a $FOR line and is treated as a normal text line. If there is text after $FOR, that text is considered part of the expression.

Nested $IF and $FOR

$IF and $FOR can be nested.

NO, Subject, Date
$FOR record, NO IN @
${NO + 1},${record.subject},${record.date}
  $IF count(record.table) > 0
    Product code, quantity
    $FOR detail IN record.table
    ${detail.product_code},${detail.quantity}
    $END
  $END
$END

Troubleshooting

Error: The condition of $IF cannot be determined. The conditional expression must result in true or false.

This error occurs when the evaluation result of the $IF condition expression is not a boolean value. In Customine’s expressions, numbers and strings are not interpreted as boolean values.

For example, the following template will result in an error:

$IF numeric
${number}
$END

Please use the operators as follows:

$IF number != 0
${number}
$END

Error: The expression for $FOR is not an array. Please specify an expression that results in an array.

The result of the $FOR expression must be in array format. This error occurs if you specify an expression that is not an array, such as a string or number.

The following is in array format so that it can be specified.

  • Data Record
  • Result value of the action to retrieve records
  • Table
  • Field values of the following field types:
    • Check box
    • Multi-choice
    • Attachment
    • User selection
    • Department selection
    • Group selection
    • Assignee
    • Category

Error: “$END not found.”

This error occurs when a corresponding $END for $IF or $FOR is not found. It can also be said that there are not enough $END for the number of $IF or $FOR. Please review the template and check if $IF to $END and $FOR and $END are correctly matched.

If the number of $IF and $FOR seems to match the number of $END, it is possible that the line with $END is not being interpreted as $END. If you write text before and after $END, that line will not be considered as $END and will be interpreted as a normal text line.

The lines of $FOR and $IF are output as they are.

$FOR and $IF must be written at the beginning of the line, excluding spaces. If there is text before $FOR or $IF on the same line, that line is considered a normal text line rather than $FOR or $IF. For example, the following template will not be considered as a $FOR statement and will be output as is.

This $FOR will be output as is, not as $FOR.

$END is outputted.

There may be too many $END compared to the number of $FOR and $IF. Please check the correspondence between $FOR, $IF, and $END.

Please verify if the error “the lines of $FOR and $IF are output as they are” occurs.

The expression should be correct with $FOR and $IF, but an expression error occurs.

Is there text written after the expressions “$FOR” and “$IF”? The text is considered as one expression, including that text.

For example, if there is an $IF like the following, score > 0 pass is considered as one conditional expression. This is not valid as an expression, so “[,] is not supported.” This message might be difficult to understand.

$IF score > 0 pass

Restrictions

  • The template only supports formats that can be interpreted as plain text. Rich text and Excel formats are not supported.

    • HTML and CSV can be used as templates.
  • The template only supports UTF-8 character encoding. Other character encodings are not supported.

  • @this and @out cannot be used. These can only be used for field mapping.

  • You can choose to output information on a line-by-line basis using the $IF command. It is not possible to separate the output or not for a part of a line using $IF. If you want to make such a separation, please use the If function.

  • Text templates can only be used in “Generate Text from Template” (Kintone app customization version / Job Runner version). It cannot be used within normal string parameters.