
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2020 03:20 PM
I've got a requirement to display some information to the user on a service catalog form showing numerical values, and also doing some math on values to display some read only fields to show the user current and revised contract amounts.
I have one read only field pulling data from a reference value on the form (contract_amount), and then using this value added together with the user entry from another field (u_change_order_amount) to display in another read only field (revised_contract_total). Using the script below in an onChange catalog client script, adding fields together works fine:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Add current contract amount and change order amount to get revised contract total
var co = g_form.getValue('u_change_order_amount');
var ca = g_form.getValue('contract_amount');
var total = parseInt(ca) + parseInt(co);
g_form.setValue('revised_contract_total', total);
}
(please ignore the form layout, still in dev 🙂 )
However part of the issue is that I need to display the values in a format that looks more like currency (I wish there was a currency type field in service catalog) so that it is easier to read and catch typos. I've used a script on other catalog variables that don't require math to format them in the past, but that causes issues adding the fields together in this case
Formatting Script (removes $ as this causes issues when mapping to a currency field on a record):
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
//Format entered data
var result = formatNumber(newValue);
if(result.search("$")){
//alert("The value for Contract Amount must be in the format of xxxx.xx");
result = result.replace("$","");
}
// To avoid recusrion
if (result != newValue) {
g_form.setValue('u_change_order_amount', result);
}
}
function formatNumber(x) {
// Regular expression allowing only numeric values
var reg = /^[\d,$.-]+$/;
if(!x.match(reg))
return false;
else
return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); // Insert commas at correct position
I think that if fields are formatted like this, part of the script doing the math would need to strip out any commas, perform the addition, and then a formatting script like this would reformat it with commas inserted again for readability. I'd also prefer to change my formatting script to display two decimal places, and I've tried using .toFixed(2) but have never gotten it to work quite right.
Has anyone had issues like this and been able to make variables perform more like a currency field in service catalog?
Solved! Go to Solution.
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2020 01:36 PM
Hi There,
Try to use the below code in your client script. This might solve the issue you have.
var co = g_form.getValue('u_change_order_amount');
var newCo=co.replace(/\,/g,"");
var ca = g_form.getValue('contract_amount');
var newCa= ca.replace(/\,/g,"");
var total = parseInt(newCo) + parseInt(newCa);
g_form.setValue('revised_contract_total', total.toString().replace(/\B(?<!\.\d*)(?=(\d{3})+(?!\d))/g, ","));
Please mark this answer as correct and helpful if it resolve the query or if it helped you to lead
into right direction then mark it helpful alone.
Thanks,
Mohit Kaushik
Mohit Kaushik
ServiceNow MVP (2023-2025)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2020 06:48 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2020 12:33 PM
Thanks for the link, unfortunately what I'm doing doesn't involve price fields or currency conversion, just need to add two string type fields together and display the result in a format that looks like currency.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-18-2020 10:52 PM
Hi,
Please use below script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
//Type appropriate comment here, and begin script below
var B = g_form.getValue('Field 1');
var A = g_form.getValue('Field 2');
//Enter the value of each field
var A1 = parseInt(A);
var B1 = parseInt(B);
var add = A1+B1;
// Add both the fields of A & B
g_form.setValue('Field 3', add);
alert(add);
}
Select as Helpful and Mark as solution if you get answers

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-19-2020 12:18 PM
When using the script I end up getting a value that adds the amounts, but only up to the comma in one of the fields.
value A = 200,000
value B = 200
total = 400