Servicely Capability
Reports and Dashboards
Template Reports
13 min
template reports are a way to generate tabular reports based on a template, which can be in the form of a word or excel document data is extracted using a script and then is made available to to the template engine where substitution and control flow operators within the template can work with the extracted data to populate the rows and columns of the report a pdf is then generated from the template template reports can be scheduled for generation along with other reports and then emailed to individual users and groups a context can be provided by the scheduled report to each of the reports and template reports that it contains to provide more information to the data extraction process this allows a generic template report to be written with the scheduled report providing the specific detail required for a specific run of the template an example might be a report for a child table of work, where the specific table name comes from the context note that this is accessible by the “template report” functionality, not “ report template https //docs servicely atlassian net/wiki/spaces/sd/pages/2284879873/reports#template ” which is where you configure template used to render reports template report to create a report, navigate to reporting > reports > new template report the following shows the navigation menu selections you may see as an administrator a template report has the following fields some of the fields are only used when the application property system multilingual is true field purpose notes name name of the report localizedname localized name of the report this is used in preference to name when system multilingual is true description description of the report localizeddescription localized description of the report this is used in preference to description when system multilingual is true key key to use instead of id in some context use cases template an attachment that defines the structure of the report common formats are a word document or an excel spreadsheet script generates report data has access to a context to specialise the generated data example #1 displaying information from multiple records this is an example of a template report with a word document template and a script that uses the context note due to the fact that this relies on querying data, we strongly advise against pulling more than a certain amount of data in once this is typically focused on daily, weekly or monthly reports, where as the amount of data more than that potentially would cause performance issues if it is a large number of records when being run therefore including queries for a created on / closed at date is strongly suggested var tableobjecttodisplayarr = \[]; // by default this report displays some incidents let records = table("incident") maxresults(5) query(); // if the context contains an id and table we display that record instead 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 script extracts the first 5 incident records, unless the context customises it to display a single record from a child table of work the extracted data is a json object representing tabular data and has this shape { record \[ { number "inc123", shortdescription "xxx", assignee "fred smith", createdon "mon, jul 17, 2023 2 17 43 pm", priority "p3 | medium", classification "other" } ] } the word document contains rows in a table structure that provide information to the template processor for how generate the content in the report daily ticket summary number short description assignee created on priority type classification {d record\[i] number} {d record\[i] shortdescription} {d record\[i] assignee} {d record\[i] createdon} {d record\[i] priority} {d record\[i] type} {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] type} {d record\[i+1] classification} content of word template extracted data is always supplied to the template processor as the variable "d" , and what this example shows is an iteration pattern if an access is made the the data structure using the pseudo variable "i" and the next row has access with "i" + 1 then that is interpreted as iterating through the array that is being indexed, generating a table row at a time note if you want to display a multi line text field, to preserve the newline characters, you will need to add \ convcrlf in the word template’s variable reference name example below {d multi line description\ convcrlf} example #2 displaying information from a single record this is similar to the previous example but you do not need to structure the answer object the same way example of the set up below template script // if the context contains an id and table we display that record instead 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; word template running a template report there is a table operation available on the template report form that allows a report to be run manually this will display a modal window that allows you to enter a context for the template report for the example shown above the context might contain answer = { table "incident", id "cc91c0bd245811eebc251e79e3c7c1bb" }; which results in the generated pdf table operations you can run a template report of a table operation if you for example, need to export a certain record into a templated pdf document after setting up the template report, you’ll need to setup a table operation onto the table you need and have the following in your table operation’s script the context to pass to the template report the navigate viewtemplatereport() function call with the template report’s key as its first parameter and the context as the second example below if there is a template report with key “servicelyreport” let context = { id current getid(), table current gettablename() }; navigate viewtemplatereport("servicelyreport", context); note that the table operation needs to run on the client side when you click on the table operation, it should setup the context and then pass them onto the template report that you targeted in the table operation template reports and scheduled reports a template report can be generated as part of a scheduled report, by selecting it from the multi choice template reports field the context can be supplied by the context script on the advanced tab a context object can be supplied to all reports, or one can be constructed that provides default values and an overrides section that uses the template report key or the template report id to distinguish one override from another in this example all other reports in the scheduled report look at an incident record, while the template report with key sampletemplatereport looks at a change record answer = { default { table "incident", id "cc91c0bd245811eebc251e79e3c7c1bb" }, overrides { sampletemplatereport { table "change", id "4e24e07d247f11ee8bbde66d9ebc34c6" } } } the scheduled report will generate an email with pdf attachments for each template report