This widget is only available for document templates.
The Functions on Objects widget allows you to use a TypeScript function to compute values which will be inserted when the document is generated from the template.
You can connect the function with input parameters which are passed at generation time. Functions on Objects sections can also be nested in generator sections like the Section generator or Table row generator; this enables you to compute separate values for each object in an object set. We recommend configuring batched functions inside generators.
Batched functions are functions that accept an object set as an argument and return results for each object in the object set. Users can configure batched functions for Functions on Objects sections that are nested inside generator sections.
Here is an example of a Functions on Objects section configured with a batched function:
Using batched functions significantly reduces the time taken to generate a document from a template. This is because of the overhead cost for executing functions: unbatched functions must execute once for each object in an object set, whereas a batched function executes only once for the entire object set.
When configuring a Functions on Objects section with a batched function, the object set argument must be configured with Object set from generator
. This template input refers to the object set that is configured for the surrounding generator section.
Batched functions may have arguments in addition to the object set argument.
Experienced Foundry users may be familiar with function-backed properties. Batched functions are similar to function-backed properties.
A batched function takes an object set as an argument and returns a FunctionsMap
that maps each object in the object set to the corresponding result. For more details, see the section on accepted function inputs and outputs.
For example, consider an unbatched function that takes in a Flight Alert object and calculates the urgency of the alert based on its delay:
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13
@Function() public getAlertUrgency(flightAlert: FlightAlertWorkshopTutorial): string { const hoursDelayed = flightAlert.timeOfDelayHours if (hoursDelayed! > 4) { return "High"; } else if (hoursDelayed! > 2) { return "Medium"; } else { return "Low"; } }
A batched version of this function takes in an object set of FlightAlertWorkshopTutorial
objects and returns a FunctionsMap<FlightAlertWorkshopTutorial, string>
. The returned FunctionsMap
contains a mapping of FlightAlertWorkshopTutorial
objects to their urgency.
Copied!1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
@Function() public flightAlertCalculateUrgency(flightAlerts: ObjectSet<FlightAlertWorkshopTutorial>): FunctionsMap<FlightAlertWorkshopTutorial, string> { const map = new FunctionsMap<FlightAlertWorkshopTutorial, string>(); flightAlerts.all().forEach(flightAlert => { const hoursDelayed = flightAlert.timeOfDelayHours if (hoursDelayed! > 4) { map.set(flightAlert, "High") } else if (hoursDelayed! > 2) { map.set(flightAlert, "Medium") } else { map.set(flightAlert, "Low") } }); return map; }
Unbatched functions must return one of the following types in order to be used in Notepad templates:
Valid batched functions must have at least one argument of type ObjectSet<K>
, and must have a return value of type FunctionsMap<K, V>
. The type V
must be one of the accepted types listed below:
Functions with sets or lists as arguments currently do not qualify as valid batched functions.
An embedded Functions on Objects section can also be connected to a generator section in this way.