Sum Table Field Values
Calculating total amounts from tables is often required when integrating with ERP systems. Using live fields to perform this task (versus a GlobalAction workflow or SQL job) gives the user instant visibility, versus allowing those calculation to happen behind the scenes. Using a Live Field also is a more supportable approach versus a SQL task, removing dependencies on external factors.
This rule expects a table field named Line Items, a field (header) to receive the summed values named Amount, and a column in the table to sum named Extended Amount.
This rule also demonstrates the use of the GlobalSearch notification panel. $$inject.notify.warn
exposes the ability to add your own messaging as a notification.
Remember that Live Field Rules only fire when the document is loaded or reloaded. While this specific rule doesn’t account for it, it is possible to save field changes made by a rule automatically when the document is opened in the viewer using $$inject.save()
.
var tf = $$inject.tableFields['Line Items'];
// Notify instead if no table field rows on document.
if(!tf.$$rowCount) {
$$inject.notify.warn("No table field rows, can not calculate a total amount.");
return "No table field rows."
}
// Set the amount field to the total of the Extended Amount col values.
$$inject.fields['Amount'] = tf.columns['Extended Amount'].$$sum;
// Set the LiveField output display value.
return 'Updated total amount.';