Calculating a currency field through a client scrip

tomsavagar
Kilo Contributor

We used to have the express version of Service Now and recently upgraded to the enterprise version and I am trying to get my head around scripting. Got the basics but having problems setting a currency field through a client script on change of another field.

Basically we have approved hours (u_project_time) which is an integer field and we need to multiply by 50 (currently not set in a field) to get the cost for the project in u_project cost. Script using is below, it works to update another integer but fails to update the currency field "onChange script error: TypeError: value.indexOf is not a function function". Sure I am just missing something simple but cant find anything which works from searching other peoples posts. 

Any help will be appreciated.

Thanks Tom (script below)

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading || newValue === '') {
return;
}

//Sets project cost value


g_form.setValue('u_project_cost',parseInt(g_form.getValue('u_project_time'))*50);

1 ACCEPTED SOLUTION

Dylan Mann1
Giga Guru

Hey Tom, 

Give this a try:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   //Sets project cost value
   var currency_amount = parseInt(g_form.getValue('u_project_time'))*50);
   var currency_code = 'USD';

   g_form.setValue('u_project_cost', currency_code + ';' + currency_amount);
}

Let me know if this helps or if you have any questions,

Dylan

View solution in original post

1 REPLY 1

Dylan Mann1
Giga Guru

Hey Tom, 

Give this a try:

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
   if (isLoading || newValue === '') {
      return;
   }

   //Sets project cost value
   var currency_amount = parseInt(g_form.getValue('u_project_time'))*50);
   var currency_code = 'USD';

   g_form.setValue('u_project_cost', currency_code + ';' + currency_amount);
}

Let me know if this helps or if you have any questions,

Dylan