The Zurich release has arrived! Interested in new features and functionalities? Click here for more

Calculate difference between 2 decimal fields and populate in other field

maneesh3
Tera Contributor

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

 

 

1 ACCEPTED SOLUTION

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();
}

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

View solution in original post

4 REPLIES 4

Pavankumar_1
Mega Patron

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);

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

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,

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();
}

 

If it helps please click Accept as Solution/hit the Thumb Icon.
ServiceNow Community MVP 2024.
Thanks,
Pavankumar

OlaN
Giga Sage
Giga Sage

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