Scripting in Report Templates
Overview
This page has advanced features that require a knowledge of JavaScript. The scripting is done in the Script window accessed through the Edit Script button of the Report Template Update page.
Details
In expressions (created by using Expression dialog or using <%= %> syntax in the template) the following short-hand is allowed to reference variable values:
<%= #code %> Â results in the variable's value or blank if the value is null.
<%= #code1 + #code2 %> Â Â if the values are numeric, will result in the two numbers being added together, otherwise the two values will be concatenated together.
In expressions, script, or script blocks
Variable Type | Missing value treated as null or Variables without a missing value definition | Missing value displayed |
---|---|---|
Numeric | #code.displayValue.value | #code.value.value |
Numeric calculation | #code.displayValue.value | |
Categorical value (e.g. 0, 1) (radio button or picklist) | #code.displayValue.value | #code.value.value |
 Categorical name (eg. no, yes) (radio button or picklist) | #code.displayValue.name |  #code.value.name |
Date | To display a date: #code.displayValue.toString() To use a date in a calculation: #code.value.value | |
Text / memo | #code.displayValue.toString() | |
Checkbox | #code.value.value |
#code.displayValue.toString() displays:
- The variable's value, or
- Blank if the value is null or defined as a missing value
#code.value.toString() displays:
- the variable's value (even if the value is defined as missing) or
- blank if the value is null.
#code.displayValue.value displays:
- Numeric variables:Â returns a number
- Date variables:Â returns a date
- All other variables:Â returns a string representation of the value
- Missing values or null:Â returns null
#code.value.value displays:
- Numeric variables:Â returns a number
- Date variables:Â returns a date
- Â All other variables:Â returns a string representation of the value
- Missing values:Â returns the missing value
- Null:Â returns null
#code.value.value - if the value is for a numeric variable, the result is a number; if the value is for a date variable, the result is a date; otherwise the result is a string representation of the value. If the value is null, the result is null. Note, values defined as missing are returned (i.e., they are not returned as null).
#code.displayValue.name - returns the picklist display name associated with the variable's value. If the value is null or defined as a missing value, empty string is returned.
#code.value.name - returns the picklist display name associated with the variable's value even if the value is defined as missing. If the value is null, empty string is returned.
Detailed explanation
#code - refers to an object with the following properties: variableId, definition, displayValue, value.
1) variableId: Integer key of the variable (used as input to some methods on the Encounters collection).
2) definition: an object that has properties and method:
               a) name: variable name.
               b) description: variable description.
3) displayValue and value return an object with the following properties. The object returned for displayValue treats values defined as missing as being null.
               a) name: picklist display name associated with the variable or empty string if null.
               b) value: the variable's value as the variable's type. Thus, numeric values are returned as number or NaN if null; date values are returned as Date objects or null; and other other data is returned as a string. If the value is null, null is returned so statement like "if (#code == null)" will be true if the value is null.
               c) IsNull: returns true if the value is null.
               d) toString(): returns a string representation of the value or empty string.
Examples
Output a variable value:
<% Output.Write( #code.displayValue.toString() );
Output a variable value even if it is defined as missing:
<% Output.Write( #code.value.toString() );
Output a sum of two numeric variables:
<% Output.Write( #code1.displayValue.value == null || #code2.displayValue.value == null ? '' : #code1.displayValue.value + #code2.displayValue.value ) %>