- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2024 03:16 PM
I'm developing a client script in my PDI on the Incident Table. When a user changes the state to canceled (value = 8), then currency 1 field gets auto populated with the absolute value of currency 2 field. Screenshot below:
Below is my Client Script that I'm using that works because it is auto populating the value, but not the absolute value. When I add Math.abs(), what's returning is NaN. So I commented that code out. Any idea what I'm doing wrong?
Much thanks!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2024 08:51 AM
Updated and working Script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var abc = Math.abs(parseFloat(g_form.getValue('u_currency_type').split(';')[1].replace(/,/g, "")));
alert(abc);
g_form.setValue('u_cur','USD'+';'+abc);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2024 07:05 AM
Hello @Phong Lo ,
Could you please upload the screenshot of latest changes. Then we may get some idea about the problem.
Thank you
Thank you
G Ramana Murthy
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2024 07:11 AM
Sure. Here are the latest changes:
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2024 07:37 AM
Hello @Phong Lo ,
you can try this
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var currency_2_value = g_form.getValue('u_currency_2').split(';');
var absoluteValue = Math.abs(parseFloat(currency_2_value[1]));
alert(absoluteValue);
//Type appropriate comment here, and begin script below
}
currency field returns value as USD;-75.46 format. So we are splitting the value using ; (semicolon)
Please mark my answer as helpful, if it helps you
Thank you
Thank you
G Ramana Murthy
ServiceNow Developer
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2024 07:47 AM
For some reason, my Math.abs() isn't working. It's still bringing up the value as NaN. Once I remove the Math.abs(), the value is -XX.XX, but not the absolute value.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-07-2024 07:49 AM - edited 02-07-2024 07:52 AM
Hello @Phong Lo ,
Please try above code, its working.
Math.abs() returns NaN, why because Currency field returns value as a USD;-xx:xx format. if we parse that value it returns NaN. So that we are splitting the value using semicolon (;) and taken part value which is at index 1 (USD at in index 0 and -xx:xx value at index 1).
Thank you
G Ramana Murthy
ServiceNow Developer