Help with currency formatting

Marcel H_
Tera Guru

I am having trouble with a script that I am trying to write to add two different currency fields together and then display the SUM in a 3rd field. Part of the issue, I think, is that once of the fields is read only and the other isn't (by design).

The fields look like the below picture on my form. I want to add Total Cost and Commission Amount together into Cost + Commission. 

find_real_file.png

The script I have is returning the following values:

  • Total Cost: 1158889
  • Commission Amount: 173833.24
  • Total: 1332722

I am using the following script in an onChange client script to calculate the amount, which is getting calculated mostly correct, I just don't get any of the commas and don't get any values after the decimal point.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
	
	var tc = g_form.getValue('total_cost');
	var ca = g_form.getValue('u_commission_amount.display').replace(/,/g, "");
	//var cc = 'u_cost_commission.display';
	var total = parseInt(tc) + parseInt(ca);
g_form.setValue('u_cost_commission.display', total.toFixed(2));  
}

Is there a way I can modify my script to get the correct output into the field?

 

7 REPLIES 7

Hi marcel,

Did u find any solution to this ?

 

Swapnil Kumar
Tera Expert

Hi Hager,

ServiceNow introduced a new feature known as "Function fields", Its a perfect solution for your requirement

In real time it will calculate the sum and update in the field, Its a way faster than calculated values

Please refer below link for More information.

Link : Function fields

 

Please mark helpful if you find this solution useful

Thanks for the suggestion, I created a new function field on the table and used the following function definition:

glidefunction:add(total_cost, u_commission_amount)

 

This adds the fields perfectly, and when I created the field I set the type to Currency, so it's displaying the correct format, but when either of the fields changes value, the function field doesn't update. To test I did the following:

Total cost = $1,158,889.00

Commission amount = 173,833.24

Function field = $1,332,722.24 (CORRECT)

 

I then changed the Commission amount field to 173,833.00 and the Function field = $1,332,722.24, but it should be $1,332,722.00

 

Are function fields a one-shot calculation that can't be updated after the first calculation is performed?