- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-16-2023 10:10 AM
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);
}
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-17-2023 10:02 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 05:27 PM
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));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 05:59 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 04:33 PM
I see that problem in the client script (it only works for totals < 1000). Seems to me that the two values need to have the 'thousands separator' removed, then add the two values, and then add the 'thousands separator' back to the result. That is the challenge with currency fields. And multiple 'thousands separators' need to be supported. Adding that logic is not trivial.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 05:27 PM
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));
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 05:59 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-18-2023 10:29 PM
You're welcome.
And am sorry, I haven't even noticed that it is not setValue (though when verifying the solution I did use setValue) 🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-19-2023 07:05 AM
Hi -O-, chris5,
It is great to find a solution, I searched and found some previous posts here without a clear solution. And I wasted time on a work-around.