Template report scripts
The script, data shape, template placeholders and context objects a template report uses, with two worked examples.
This reference shows how a template report’s script, its template and its context fit together. Use it while you follow Create a template report.
How the parts connect
- Script: server script that extracts data and sets
answerto an object. It can read thecontextobject. - Data: the object in
answer, passed to the template processor as the variabled. - Template: a Word or Excel document whose placeholders, such as
{d.record[i].Number}, read fromd. - Context: an object supplied when the report runs, from the run dialog, a table operation or a scheduled report’s Context script.
Example 1: Several records in a table
This script lists five incidents by default. If the context names a table and record, it lists that record instead, which lets one template serve any child table of Work.
var tableObjectToDisplayArr = [];
let records = Table("Incident")
.maxResults(5)
.query();
if (context.id && context.table) {
records = [Table(context.table, context.id)];
}
records.forEach((ticketRec) => {
let individualTicketDetail = {};
individualTicketDetail.Number = ticketRec.Number();
individualTicketDetail.ShortDescription = ticketRec.ShortDescription();
individualTicketDetail.Assignee = ticketRec.Assignee() ? ticketRec.Assignee.getDisplayValue() : "";
individualTicketDetail.CreatedOn = ticketRec.CreatedOn.getDisplayValue();
individualTicketDetail.Priority = ticketRec.Priority.getDisplayValue();
individualTicketDetail.Classification = ticketRec.Classification.getDisplayValue();
tableObjectToDisplayArr.push(individualTicketDetail);
});
answer = { record: tableObjectToDisplayArr };
The data has this shape:
{
record: [
{
Number: "INC123",
ShortDescription: "Cannot connect to the VPN",
Assignee: "",
CreatedOn: "Mon, Jul 17, 2023 2:17:43 PM",
Priority: "P3 | Medium",
Classification: "Other"
}
]
}
The Word template holds a table titled “Daily Ticket Summary” with a header row and two placeholder rows:
| Number | Short Description | Assignee | Created on | Priority | Classification |
|---|---|---|---|---|---|
{d.record[i].Number} |
{d.record[i].ShortDescription} |
{d.record[i].Assignee} |
{d.record[i].CreatedOn} |
{d.record[i].Priority} |
{d.record[i].Classification} |
{d.record[i+1].Number} |
{d.record[i+1].ShortDescription} |
{d.record[i+1].Assignee} |
{d.record[i+1].CreatedOn} |
{d.record[i+1].Priority} |
{d.record[i+1].Classification} |
This is the iteration pattern. A row that reads the array with i, followed by a row that reads it with i+1, makes the processor generate one table row per array item.
Example 2: One record
For a single record, the data does not need an array. Set fields on answer directly and reference them in the template, for example {d.number}.
let answerObj = {};
if (context.id && context.table) {
record = Table(context.table, context.id);
answerObj.number = record.Number();
answerObj.short_description = record.ShortDescription();
answerObj.updated_on = record.UpdatedOn.displayValue();
if (record.Source.hasValue()) {
answerObj.source = record.Source.displayValue();
} else {
answerObj.source = "";
}
}
answer = answerObj;
Multi-line text
To keep the line breaks of a multi-line text field, add :convCRLF to the placeholder, for example {d.multi_line_description:convCRLF}.
Context objects
A context for one record names its table and ID:
answer = {
table: "Incident",
id: "<record id>"
};
A scheduled report’s Context script can supply one context to every report, or defaults with overrides. An override is keyed by a template report’s key or ID. In this example every report looks at an incident, except the template report with key SampleTemplateReport, which looks at a change.
answer = {
default: {
table: "Incident",
id: "<incident record id>"
},
overrides: {
SampleTemplateReport: {
table: "Change",
id: "<change record id>"
}
}
};