How to add two currency fields together

chrish5
Giga Guru

Hi Community,

I need to add two currency fields together to get a value for a third currency field.  Below is my  onChange client script, but it is not giving me the expected results.  Any advice would appreciated.   Thanks!

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var cb = g_form.getDecimalValue('capital_budget');
var ob = g_form.getDecimalValue('operational_budget');
var total = (cb) + (ob);
g_form.setDecimalValue('u_total_planned_budget', total);
}

 

 

chrish5_0-1673891759472.png

 

3 ACCEPTED SOLUTIONS

Bert_c1
Kilo Patron

the challenge here is that the fields are Currency fields. I did get the following logic to work in my test of a client script:

 

   //Type appropriate comment here, and begin script below
	var cb = g_form.getValue('capital_outlay');
	var ob = g_form.getValue('operational_outlay');
	var cbStr = cb.toString();
	var obStr = ob.toString();
	var semiPos = cbStr.indexOf(";");
	var cbValue = cbStr.substring(semiPos+1);
	var semiPos1 = obStr.indexOf(";");
	var obValue = obStr.substring(semiPos1+1);
	var total = Number(cbValue) + Number(obValue);
	g_form.addInfoMessage("cb=" + cb + ", ob=" + ob + ", cbStr=" + cbStr + ", obStr=" + obStr + ", cbValue=" + cbValue + ", obValue=" + obValue + ", total " + total);
//	g_form.addInfoMessage("obStr=" + obStr);
	var totalVal = "USD;" + total.toString();
	g_form.setValue('u_total_planned_budget', totalVal);

I used 'capital_outlay and operational_outlay as those are not read-only in my instance.  The problem here is that a 'currency' "USD" is specified. I don't know that will meet your needs.  the system property in my instance named 'glide.i18n.single_currency.code' is "USD".  I suppose using the value of the system property will work. You need to test.

 

the following server-side script works (tested in scripts background):

var dmn = new GlideRecord('dmn_demand');
dmn.query();
while (dmn.next()) {
	gs.info("capital_outlay = " + dmn.capital_outlay + ", operational_outlay = " + dmn.operational_outlay + ".");
	gs.info("capital_budget = " + dmn.capital_budget + ", operational_budget = " + dmn.operational_budget + ".");
	var co = new Number(dmn.capital_outlay);
	var oo = new Number(dmn.operational_outlay);
	var cb = new Number(dmn.capital_budget);
	var ob = new Number(dmn.operational_budget);
	var totalOutlay = co + oo;
	var totalBudget = cb + ob;
	gs.info("totalOutlay = " + totalOutlay + ", total budget = " + totalBudget + ".");
	dmn.u_total_planned_budget = totalBudget;
	dmn.update();
}

Anyway, I hope I have provided something useful.

View solution in original post

-O-
Kilo Patron
Kilo Patron

You just need to formatNumber the value that is to be set:

function onChange (control, oldValue, newValue, isLoading) {
	if (isLoading || newValue == '') {
		return;
	}
	var cb = g_form.getDecimalValue('capital_budget');
	var ob = g_form.getDecimalValue('operational_budget');
	var total = (cb) + (ob);
	g_form.setDecimalValue('u_total_planned_budget', formatNumber(total));
}

View solution in original post

Hi -O-,

Thanks for the advise. This worked.  I did have to change the last line back to g_form.setValue instead of g_form.setDecimalValue.  It was giving me an error on the form that setDecimalValue was not a function.  

View solution in original post

9 REPLIES 9

surbhi9
Tera Contributor

You have to write two onChange client scripts, one for Capital budget field and another for Operational budget field.

 

Please mark helpful/correct, if resolved.

Thanks!!

I did as you suggested using the below script for both onChange client scripts, onefor capital_budget changing and one for operational_budget changing.  The total_planned_budget field is calculating, but it's only adding the capital_budget field and only when that capital_budget changes.  It's ignoring the operational_budget field.   If I inactive the onChange client script for capital_budget, it is still only adding the capital_budget field, but only after the operational_budget field changes.  Thoughts? 

 

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var cb = g_form.getValue('capital_budget');
var ob = g_form.getValue('operational_budget');
var total = (cb) + (ob);
g_form.setValue('u_total_planned_budget', total);
}

Bert_c1
Kilo Patron

the challenge here is that the fields are Currency fields. I did get the following logic to work in my test of a client script:

 

   //Type appropriate comment here, and begin script below
	var cb = g_form.getValue('capital_outlay');
	var ob = g_form.getValue('operational_outlay');
	var cbStr = cb.toString();
	var obStr = ob.toString();
	var semiPos = cbStr.indexOf(";");
	var cbValue = cbStr.substring(semiPos+1);
	var semiPos1 = obStr.indexOf(";");
	var obValue = obStr.substring(semiPos1+1);
	var total = Number(cbValue) + Number(obValue);
	g_form.addInfoMessage("cb=" + cb + ", ob=" + ob + ", cbStr=" + cbStr + ", obStr=" + obStr + ", cbValue=" + cbValue + ", obValue=" + obValue + ", total " + total);
//	g_form.addInfoMessage("obStr=" + obStr);
	var totalVal = "USD;" + total.toString();
	g_form.setValue('u_total_planned_budget', totalVal);

I used 'capital_outlay and operational_outlay as those are not read-only in my instance.  The problem here is that a 'currency' "USD" is specified. I don't know that will meet your needs.  the system property in my instance named 'glide.i18n.single_currency.code' is "USD".  I suppose using the value of the system property will work. You need to test.

 

the following server-side script works (tested in scripts background):

var dmn = new GlideRecord('dmn_demand');
dmn.query();
while (dmn.next()) {
	gs.info("capital_outlay = " + dmn.capital_outlay + ", operational_outlay = " + dmn.operational_outlay + ".");
	gs.info("capital_budget = " + dmn.capital_budget + ", operational_budget = " + dmn.operational_budget + ".");
	var co = new Number(dmn.capital_outlay);
	var oo = new Number(dmn.operational_outlay);
	var cb = new Number(dmn.capital_budget);
	var ob = new Number(dmn.operational_budget);
	var totalOutlay = co + oo;
	var totalBudget = cb + ob;
	gs.info("totalOutlay = " + totalOutlay + ", total budget = " + totalBudget + ".");
	dmn.u_total_planned_budget = totalBudget;
	dmn.update();
}

Anyway, I hope I have provided something useful.

Hi and thanks for your suggestion!   This works until capital budget or operation budget has a value with a "," in it like "1,000.00".  This will likely happen.  

 

If I do capital budget of 100.00 and operational budget of 200.00, total planned budget is 300.00

cb=USD;100.00, ob=USD;200.00, cbStr=USD;100.00, obStr=USD;200.00, cbValue=100.00, obValue=200.00, total 300

 

If I do capital budget of 1,000.00 and operational budget of 200.00, then it does not calculate Total Planned Budget.  

cb=USD;1,000.00, ob=USD;200.00, cbStr=USD;1,000.00, obStr=USD;200.00, cbValue=1,000.00, obValue=200.00, total NaN

 

How do we account for the ","?   And there could be multiple "," in the budget like 1,000,000.00.