record
The record object gives access to the record that is currently being merged with the template.
Properties
Field |
Type |
Description |
---|---|---|
<fieldName> |
Object |
Shortcut to access a field in the fields array. For example, you can use Note: If a record has a field and a table of the same name, |
fields |
Array |
The field values that belong to this record. |
id |
Number |
The id of this record. |
index |
Number |
The one-based index of this record, or zero if no data is available. |
<tableName> |
Array of records |
Shortcut to access a table in the tables array. For example, you can use |
tables |
Array |
The detail tables that belong to this record. |
Accessing fields and tables
You can access a specific field value:
-
using the field's name:
record.fieldName
(case sensitive) -
via the
fields
array, by name:record.fields.fieldname
orrecord.fields['fieldname']
(case insensitive)
If there is no field with the specified name, record.fields.fieldname
returns an empty string, whereas record.fieldname
will return undefined
(unless there is a table with the same name, in which case the table is returned).
A detail table can be accessed using its name: record.tableName
, or via the tables
array. Either way the table name should be followed by a numeric index for a record in that detail table.
For example, to access the value of the field "prod_id" in the first record of a detail table called "detail", you can use:
record.detail[0].fields.prod_id or
record.tables["detail"][0].fields.prod_id or
record.tables.detail[0].fields.prod_id or
record.detail[0].prod_id
If there is no table (and no field) with the specified name, record.tableName
results in undefined
.
In order to loop over records in a detail table you could use a for(... in ...) loop (see for(... in ...)), for example:
var records = record.tables.detail;
for (var i in records) {
var rec = records[i];
...
}
Alternatively you could use an 'Each matched element' script (see Setting the scope of a script).
Yet another way is to create a standard for
loop using the table's length
property:
var records = record.tables.detail;
for (var i = 0; i < records.length; i++) {
var rec = records[i];
...
}
Examples
record.fields
The following Standard Script evaluates the data field Country in the current record. If the value is 'CANADA' it will show the results, otherwise it will hide them. (The results object contains the elements that match the script's selector; see results and Writing your own scripts.)
if (record.fields["Country"] == "CANADA") {
results.show();
} else {
results.hide();
}
Instead of record.fields["Country"]
you could write record.Country
(case sensitive), if there is no detail table with the same name.
In a Control Script, an entire section could be enabled or disabled based on the same condition:
if (record.Country == "CANADA") {
merge.template.contexts.PRINT.sections["Section 1"].enabled = true;
} else {
merge.template.contexts.PRINT.sections["Section 1"].enabled = false;
}
(For more information about Control Scripts, see Control Scripts.)
record.tables
The next script looks up a value in the first record in a detail table called "detail" and shows or hides the results depending on that value.
if (record.tables.detail[0].fields.prod_id == "10") {
results.show();
} else {
results.hide();
}
Note that indexes start counting at 0, so tables.detail[0]
refers to the first record in the detail table.