how can sum of two variables and populate in third one on catalog form?

Chandra18
Mega Sage

Hi team,

 

There are three fields total amount, P.Amount, and L.Amount. here total amount = P.Amount + L.Amount.

All fields accept number values on the catalog form, I want if the value gets a change of any variables (P.Amount + L.Amount) the total amount value should be changed.
How can I achieve this?
Thanks

6 REPLIES 6

Hi @Chandra18,

 

just use setvalue to updated total into Total variable.

 

g_form.setValue("total_amount_variable", sum);

 

Thanks,

Sagar Pagar

The world works with ServiceNow

Sagar Pagar
Tera Patron

Hi @Chandra18,

 

Correcting my previous response. You have to create 2 On-change Catalog client scripts as -

 

Catalog Client scripts 1:

Variable - P Amount

 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var value1 = newValue; // p-amount
	var value2 = g_form.getValue("l_amount");
	value2 = value2 || 0;

	var sum = parseInt(value1) + parseInt(value2);
	g_form.setValue("total_amount", sum);

}

 

 

Catalog Client scripts 2:

Variable - L Amount

 

 

function onChange(control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}

	var value1 = g_form.getValue("p_amount"); // L Amount
	value1 = value1 || 0;
	var value2 = newValue;
	
	var sum = parseInt(value1) + parseInt(value2);

	g_form.setValue("total_amount", sum);

}

 

 

Make sure to update your variable names.

 

Thanks,

Sagar Pagar

The world works with ServiceNow