- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 09:31 AM
When using a script to calculate two currency fields and show the sum in a 3rd currency field it's not returning the proper formatting.
function onLoad(control, oldValue, newValue, isLoading, isTemplate) {
var tot= 0.00;
var f1 = g_form.getDecimalValue('capital_outlay'); //first currency field.
var f2 = g_form.getDecimalValue('labor_costs'); //second currency field
tot = f1+f2;
g_form.setValue('total_costs', "$"+tot); //Need commas and decimals to show properly;
}
Decimals are showing correctly but other numbers don't have commas. I have seen several post on this but don't know how to adapt answers to my solution. Any advise would be helpful. Thank you
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 10:25 AM
Hi Chad,
Please try the below script. I have tried and executed. The code works as expected:
var tot= 0.00;
var f1 = g_form.getDecimalValue('capital_outlay'); // f1=505.89
var f2 = g_form.getDecimalValue('labor_costs'); // f2=5000.00
tot = f1+f2;
function formatNumber (num) {
return num.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, "$1,")
}
var sum=formatNumber(tot));
g_form.setValue('total_costs',sum);
Output:
*** Script: Total::::5,505.89
Please mark the answer helpful if it helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 09:52 AM
According to the Service now docs you want to use .getDisplayValue() to work with currency.
More information can be found here
Let me know if this helps.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 10:06 AM
Thank you for your response.
The getDisplayValue() function just changed the result to testtest isntead of a numeric value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 10:11 AM
Is test the default value of your currency fields?
I just noticed that your function is onLoad() instead of onSubmit(). So the displayValue() on load would be whatever the default value of your field is.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-22-2019 10:18 AM
There isn't a default value set for those fields.
when changing it to on submit, there was no change in functionality.