Select Page

Search Knowledge Base by Keyword

Sum Function

Goal:

Calculate a sum amount from numbers inserted to TextInputs in a Document or Bundled Documents.

Instructions:

  1. Create the Template tag Sum and attach this tag to all TextInputs in your Template that should be calculated.
  2. Create the Template tag GetSumValue and attach this tag to the TextInput in your Template where the sum amount should be inserted.
  3. Insert the below-mentioned script to the Sum tag.

Script Example:

const Tags = LEGITO.documentBuilder.getTagsByName("Sum");

var finder = LEGITO.documentBuilder.event.createElementFinder();
var sumValues = finder.findElementsByTagsAuto(Tags);

const Results = LEGITO.documentBuilder.getTagsByName("GetSumValue");
var resultElement = finder.findElementsByTagsAuto(Results)[0];

let valuesArray = []
for(var i in sumValues) {
if(sumValues[i].getValue() !== null) {
valuesArray.push(sumValues[i].getValue());
}
}

let arrSum = valuesArray.reduce((a,b) => parseInt(a) + parseInt(b), 0);

resultElement.setValue(arrSum.toString());