What is the best way to show the sum of two variables in another variable when the values are currency values?

TEdwards
Kilo Sage

I have a requirement to show the results of adding two other variables (amount_budgeted + prior_car_costs) into another variable called 'total_cost'. In my research I have seen this accomplished by both catalog script and macro/widget, but have been unable to get it to work for me either way. I thought it may be due to the fact that the values of both 'amount_budgeted' and 'prior_car_costs' already have catalog scripts applied to them to require them to be currency values.

Can anyone advise whether it is better to do this by catalog script or making 'total_cost' a Macro variable and whether the fact that the values are currency makes a difference? I will be happy to provide any relevant information. Thanks! 

1 ACCEPTED SOLUTION

Tony is correct. you do need to parse out the numbers.
but when inserting your returned value into a field you need to have the symbol added to it.
Below is a working client script I used to test this.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	var currency1 = parseFloat(g_form.getValue('u_currency').split(';')[1]); //first number
	var symbol = g_form.getValue('u_currency').split(';')[0]; //The associated symbol
	g_form.addInfoMessage(currency1 + ', ' + symbol);
	
	
	var total = currency1 + parseFloat(newValue.split(';')[1]); // Current field new value
	g_form.addInfoMessage(total);
	
	g_form.setValue('u_currency_total',symbol + ';' + total); // the inserted value
	
	g_form.getValue('u_currency_total'); //see the final insert
   //Type appropriate comment here, and begin script below
   
}

View solution in original post

3 REPLIES 3

Tony Chatfield1
Kilo Patron
Hi, server and client side currency behaviour is different, with the 'currency' and use of the returned data having an impact on the way you need to retrieve it. ServiceNow Doc’s has a section specifically for currency scripting and it’s worth a quick read. https://docs.servicenow.com/bundle/newyork-platform-administration/page/administer/currency/concept/currency-values-scripts.html Client side I find this gives me a result I can manipulate as needed var myAmount = parseFloat(g_form.getValue('amount').split(';')[1]); and server side I use var myTotal = parseFloat(current.amount.getReferenceValue());

rexked
Tera Expert

If its anything like price it creates a seperate record it references.

Bit of digging it does create a record for currency in the "fx_currency_instance" table.

The ID of that record is a reference to your current record, it also has a refrence to the field you are using.

You may have to edit that existing reference record of the field your currency value is referencing.

 

 

Tony is correct. you do need to parse out the numbers.
but when inserting your returned value into a field you need to have the symbol added to it.
Below is a working client script I used to test this.

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }
	var currency1 = parseFloat(g_form.getValue('u_currency').split(';')[1]); //first number
	var symbol = g_form.getValue('u_currency').split(';')[0]; //The associated symbol
	g_form.addInfoMessage(currency1 + ', ' + symbol);
	
	
	var total = currency1 + parseFloat(newValue.split(';')[1]); // Current field new value
	g_form.addInfoMessage(total);
	
	g_form.setValue('u_currency_total',symbol + ';' + total); // the inserted value
	
	g_form.getValue('u_currency_total'); //see the final insert
   //Type appropriate comment here, and begin script below
   
}