Performing math functions within Client script

sikoras
Kilo Contributor

I have an onChange script that is based on change in an hours field is then updating the total number of hours and recalculating the dollar costs based on the new # of hours and then the total dollar costs based on the new recalculated dollar costs. I had these working perfectly as a business rule but need them to update right away instead of having t wait to do a save and then waiting for the business rule to fire.   I'm having any issue with the following (please refer to script below):

 

o - new dollar calculation and total dollar calculations are not showing the currency format (instead of showing $90.00 script is display 90

o - total hours field is displaying as an concatenation of the columns and not adding them together e.g. 102050

 

note - dollar fields are defined on the dictionary as currency fields and the hour fields are defined as integers (these are the u_ fields referenced in the script.

 

function onChange(control, oldValue, newValue, isLoading) {

 

  if (!isLoading) {

//

        if (newValue) {

//              

    var itcore = g_form.getValue('u_analysis_it_core_hours');

  var itext = g_form.getValue('u_analysis_it_external_hours');

  var itoff = g_form.getValue('u_analysis_it_offshore_hours');

  var itemp = g_form.getValue('u_analysis_it_employee_hours');

  var itextdol = g_form.getDecimalValue('u_analysis_it_external_dollars');

  var itoffdol = g_form.getDecimalValue('u_analysis_it_offshore_dollars');

  var itempdol = g_form.getDecimalValue('u_analysis_it_employee_dollars');

//  

          var itcoredol = itcore * 90;        

//

  g_form.setValue('u_analysis_it_core_dollars.getRefereneDisplayValue()',(parseFloat(itcoredol)));

//

  g_form.setValue('u_analysis_total_hours', (itcore)+(itext)+(itoff)+(itemp));

//

  g_form.setValue('u_anaysis_total_dollars.getRefereneDisplayValue()', (parseFloat(itcoredol+parseFloat(itextdol)+parseFloat(itoffdol)+parseFloat(itempdol))));

//

2 REPLIES 2

paul_mcnamara
Giga Expert

If you are trying to put float into integers that won't work well change to parseInt.



For setting Currency via client script there is a great post here



https://community.servicenow.com/people/russ.sarbora/blog/2012/07/20/2543


richfred
Mega Expert

We have created a own table with exchange rates and make calculations (and translation from each currency to the global company) the following way:



We call the values from our exchange rate table:


function readExchangeRate(localCurrency) {


    var curRecord;


    var exchangeRate;


    curRecord = new GlideRecord('u_exchange_rate');


    curRecord.addQuery('u_currency', '=', localCurrency);


    curRecord.query();


    while(curRecord.next()){


  exchangeRate = curRecord.u_rate;          


    }


    return(exchangeRate);


}



Then in the business rule we use this script to calculate the local currency to the global currency in another field:


function updateNokPrice() {


    var localPrice = current.u_local_price.toString();


    var localCurrency = current.opportunity.owner.company.u_currency.getDisplayValue();


    var exchangeRate = readExchangeRate(localCurrency);


    var nokPrice = Math.round(localPrice * exchangeRate);


    current.u_nok_price.setValue(nokPrice);


return;




Then on top of the business rule you add: "updateNokPrice();" to execute it.



This works correctly to retrieve the exchange rates, and calculate it to another currency (in a separate field so you have one field with the original currency and one with the global currency)