Simple Subtraction

rarootes
Tera Contributor

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?

1 ACCEPTED SOLUTION

yad_achyut
Giga Guru

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.


 

View solution in original post

7 REPLIES 7

Richard Hine
Tera Guru
Tera Guru

Rarootes,

Perhaps you could share what you have already done and then the community could give their insights as to why it potentially isn't working?

Thanks,

Richard

rarootes
Tera Contributor

Okay, so I'm coming back to this.

I am running this in an onChange Client Script. I have two currency fields, and need to subtract one from the other. The original values from a table referenced as an entire section on the form, and the answer is a field native to the form. This is my latest version; I've gone through numerous variations.

Standard function statement followed by:

 

var budg = g_form.getValue("x_financials.proposed_budget").replace(/,/g, "");

var cost = g_form.getValue("x_financials.total_cost").replace(/,/g, "");

var delta = parseFloat(budg) - parseFloat(cost);

g_form.setValue("x_demand.projected_margin", delta);

yad_achyut
Giga Guru

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.