---
title: "Template report scripts"
site: admin
version: 1.11
versions: "all"
url: https://docs-new.servicely.ai/admin/1.11/reporting/template-report-scripts/
type: reference
application: platform
last_updated: 2026-10-18
commit: 50111ae9bd3b
---


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](page:reporting/template-reports).

## How the parts connect

- **Script**: server script that extracts data and sets `answer` to an object. It can read the `context` object.
- **Data**: the object in `answer`, passed to the template processor as the variable `d`.
- **Template**: a Word or Excel document whose placeholders, such as `{d.record[i].Number}`, read from `d`.
- **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.

```javascript
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:

```javascript
{
  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}`.

```javascript
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:

```javascript
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.

```javascript
answer = {
  default: {
    table: "Incident",
    id: "<incident record id>"
  },
  overrides: {
    SampleTemplateReport: {
      table: "Change",
      id: "<change record id>"
    }
  }
};
```

## Related

- [Create a template report](page:reporting/template-reports)
- [Schedule a report](page:reporting/scheduled-reports)
- [Table operations](page:automation/table-operations)
