onChange Client Script does not re-calculate when rows within a MRVS are deleted

neil_b
Tera Guru

I have an onChange client script on a record producer that calculates 2 fields, the # of people attending and the total cost of a ticket. It is supposed to take the total cost of the ticket and divide it by the # of people attending. After further testing, I noticed that if I delete a record from my MRVS and add a new record (due to entering a typo in the amount field or ticket description field), the total cost per person attending is not getting recalculated. Here is my client script: 

function calcTotalCost() {
	if(newValue!="false"){
		if(g_form.getValue('ticket_price'))
		var multiRowVariableSet = JSON.parse(g_form.getValue('ticket_price'));
		var rowCost = 0;
		var totalCost = 0;
		for (var i = 0; i < multiRowVariableSet.length; i++) {
			rowCost = parseInt(multiRowVariableSet[i].total_price)/parseInt(multiRowVariableSet[i].ppl);
			totalCost = totalCost + parseInt(rowCost);
			rowCost = 0;
		}
		g_form.setValue("total_price_per_person",totalCost);
		g_form.setValue("trigger", "false"); //reset the trigger	
	}
}

Here are the images of my steps:
1) I added 1 ticket for $200 and the calculated field 'Total Per Person' displays $200 as expected.
 Initially.png
2) I deleted the row
 Delete Row.png

3) I added a new row with the correct number but the total per person did not get re-calculated. I would have expected $100 to display.
Consequently.png


How can I get this to re-calculate if records within the MRVS are deleted and re-added?

 

1 REPLY 1

Peter Bodelier
Giga Sage

Hi @neil_b 

 

You can't do that with a client script, but you should be able to do this with a widget.

 

A similar use case is described here: Solved: How to calculate the total of variables into anoth... - ServiceNow Community


Help others to find a correct solution by marking the appropriate response as accepted solution and helpful.