Count total if a item from data table

Hi
I would like to count the total of a field from a data table and check to see if the total is matching with another field in the form

please see the code I have used below.

  if (fd.field('Total').value != fd.field('TotalNumberofUnits').value) {
            this.error = "Sum of No of Units and Total Number of Units must match. Please correct and resubmit.";
            return false;
        } 


fd.beforeSave(function(data) {
     var myVar = setInterval(calcTotal, 2000);
  
    calcTotal();
    clearInterval(myVar);
    alert('Test');
});

var myVar = 0;

function calcTotal() {
 
	var value = fd.control('DataTable1').widget.dataItems();
	var total = 0;
	if(value){
	    for (var i = 0; i < value.length; i++){
	        if(value[i].NoOfUnit)
	          total += parseInt(value[i].NoOfUnit.replace(/(?!^-)[^0-9.]/g, "").replace(/(\..*)\./g, '$1'));
	    	}
		}	
	fd.field('Total').value = total;
}

But it throws an error "FormValidationError: Form is invalid"

never mind figured it out, thanks

1 Like