- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2022 07:50 AM
I have three currency fields, all on the same table. I need to subtract field B from field A and populate field C with the result.
Essentially A - B = C.
I've gone at this from multiple directions, Calculated value, BU, Client script, but none of the Community suggestions have worked for me. What would be the best way to go about this?
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎03-10-2024 06:11 PM
Hi ,
If these fields are on a Table there is no need of creating a client script you can simply perform the calculation on the field by using ServiceNow OOB functionality "Calculated Value" under advanced view where you want to get the result of the calculation.
As per your requirement you can write the code on Calculated Value for the Field "C" as :
var firstValue = currrent.A;
var secondValue = current.B;
var result = parseFloat(firstValue) - parseFloat(secondValue);
return result;
This will return the calculated value to the desired variable.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2022 08:52 AM
You can configure a client script like below to calculate difference between 2 currency fields.
var cur1 = parseInt(g_form.getValue('u_currency_1'));
var cur2 = parseInt(g_form.getValue('u_currency_2'));
var result = cur1 - cur2;
Regards,
Sachin

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2022 08:56 AM
Hey,
Use Number() to convert your variables into valid integers and then perform your operation:
Number()
converts the type whereas parseInt parses the value of input.
// Parsing
parseInt('32px'); // 32
parseInt('5e1'); // 5
// Convert type
Number('32px'); // NaN
Number('5e1'); // 50
Ref link:
https://thisthat.dev/number-constructor-vs-parse-int/
Aman Kumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2022 09:29 AM
Hi
You need to write two onchange client scripts, one on field A and one on Field B.
Write the client scripts as below.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var A = parseInt(g_form.getValue('field A name here'));
var B = parseInt(g_form.getValue('field B name here'));
var output = A - B;
g_form.setValue('field C name here', output);
}
Mark as correct or helpful if it works.
Regards,
Sumanth

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-05-2022 09:36 AM
Hi Rarootes,
As the currency field is Float type you have to use parseFloat, Check below onChange Script on Var B.
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var A = g_form.getValue('u_a.display').replace(/,/g, "");
var B = g_form.getValue('u_b.display').replace(/,/g, "");
var C = parseFloat(B) - parseFloat(A);
g_form.setValue('u_c.display', C);
}
Output:
Kindly mark correct and helpful if applicable