Add support for cancellation

This document describes the usage of version 1 of Template scripts which is deprecated. For step-by-step instructions on how to create templates with version 2 see Templating and reporting.

A template script can take time to complete, make sure it can be canceled when the user closes the Record Viewer or changes the record.

To support cancellation you should supply the cancelPromise input property to all the expensive queries of your script.

The following version of the script will pass cancelPromise to the getLinkedRecords query to allow Investigate to cancel it when required.

/**
 * Common function for data fetching, used by all views/downloads.
 */
async function fetchRecordData(input, recordData) {
  const { record, dataModelEntity, cancelPromise } = input;

  ...

  const investments = await record.getLinkedRecords(securedInvestmentsRelation, {
    size: 1000,
    orderBy: { field: 'raised_amount', order: 'desc' },
    cancelPromise // Pass the cancelPromise in the query options
  });

  ...
}

Next steps

To see the full example code, see full example code.

To have your template script produce a downloadable report, see downloadable reports.