- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2022 10:56 PM
Hi Team,
I am having an requirement to calculate difference between 2 decimal fields and display the value in other field.
if,
Field A = 10.5
Field B = 5
then
Field C = 5.5
like above I need to display kindly suggest.
Thanks
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2022 11:41 PM
Hi @maneesh3 ,
You can use fix script and background script to update the existing records.
Use below script and replace your table and fields
Script:
var gr = new GlideRecord('incident'); //give your table name
gr.addEncodedQuery('');//filter the list that you need to update difference of a and b on c and use that query here.
gr.query();
while (gr.next()) {
var result = gr.fielda - gr.fieldb;
gr.fieldc = result;
gr.setWorkflow(false);
gr.update();
}
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2022 11:14 PM - edited 10-16-2022 11:27 PM
Hi @maneesh3 ,
if you need this requirement when on change values then go with the Onchange Client scripts on field A and field B
Use below script:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}
var a = g_form.getValue('FIELD A');
var b = g_form.getValue('FIELD B');
var c = parseFloat(a) - parseFloat(b);
g_form.setValue('FIELD C', c);
}
If you need want to display when form load go with Onload client script.
Or Use can use calculate value.
1. Configure dictionary of field c and in advanced view you will have Calculated value tab check Calculated check box and use below script for calculation.
(function calculatedFieldValue(current) {
// Add your code here
var c = current.fielda - current.fieldb; //use your filed names of a and b.
return c;
})(current);
ServiceNow Community MVP 2024.
Thanks,
Pavankumar
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2022 11:31 PM
Hi PavanKumar,
Code worked for me very thanks for that. One small suggestion, how I can make it for existing records so that I can show in list view. Kindly help
Thanks,
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2022 11:41 PM
Hi @maneesh3 ,
You can use fix script and background script to update the existing records.
Use below script and replace your table and fields
Script:
var gr = new GlideRecord('incident'); //give your table name
gr.addEncodedQuery('');//filter the list that you need to update difference of a and b on c and use that query here.
gr.query();
while (gr.next()) {
var result = gr.fielda - gr.fieldb;
gr.fieldc = result;
gr.setWorkflow(false);
gr.update();
}
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-16-2022 11:37 PM
Hi,
This should be fairly simple, so what's the issue?
Example below:
var a = 10.5;
var b = 5;
var c = a-b;
gs.info('c: ' + c);
// output -->> c: 5.5