How to convert and multiply currency, glide date, and decimal?

zulhiyatiuwi
Tera Expert

Hello... I'm a newbie on servicenow. I want to ask how to convert currency, glide date and decimal to float so i can get result from multile them.

Contract Value = Currency

Penalty percentage = Decimal

Over due date = glide date --> i get the value from today - end date (onChange=today)

find_real_file.png

I want to get value breach penalty from multiple 3 field. i use on change = over due date. i make this script. but it not working. please help. i can't do script well. thank you.

find_real_file.png

1 ACCEPTED SOLUTION

This should help.



var con = g_form.getValue('u_contract_value').substring(4);


var per = g_form.getValue('u_penalty_percentage');


var total = parseFloat(con) * parseFloat(per);


g_form.setValue('breach_penalty_amount', total);


View solution in original post

13 REPLIES 13

//var con = g_form.getValue('u_contract_value'); //comment this code


alert(con); // this value is 1000 //I believe this one would be giving you like USD;1000, in that case use the below line and try,


var value = g_form.getValue('u_contract_value').substring(4);


var per = g_form.getValue('u_penalty_percentage');


alert(per); // this value is 0.1


var total = parseFloat(con) * parseFloat(per);


alert(total); // this value is NaN


alert('TEST');


g_form.setValue('breach_penalty_amount', total);


This should help.



var con = g_form.getValue('u_contract_value').substring(4);


var per = g_form.getValue('u_penalty_percentage');


var total = parseFloat(con) * parseFloat(per);


g_form.setValue('breach_penalty_amount', total);


Hello,

The incorrect value is not due to parseFloat. I have worked on this today and it is due to the the value you get for "g_form.getValue('u_contract_value').substring(4);" i.e., 1,000 and "," comes under octal literal which should not be used in the method parseFloat(). By having that "," in the value "1,000" system takes the integer value as "1" and gives the calculated output for "1". Hence you need to replace all the commas in the "g_form.getValue('u_contract_value').substring(4);"(which is currency field) and then parseFloat it to get the actual/correct output.

Solution is:

var date = g_form.getValue('u_over_due_date');

alert(g_form.getValue('u_contract_value')); 

var value = g_form.getValue('u_contract_value'); 

var value1 = g_form.getValue('u_contract_value').substring(4);//You will get 1,000 from this code

var value_final =value1.replace(/,/g, "");//Replaces all commas in "value1"

var person = g_form.getValue('u_penalty_percentage');

var total = parseFloat(date) * parseFloat(value_final) * parseFloat(person);

 

alert(total); //Gives correct output But not NaN

 

Can you also mark the answer as correct and helpful to help future searches.

Regards,

Lini Aadhi

Lini Aadhi
Tera Contributor

Hello,

The incorrect value is not due to parseFloat. I have worked on this today and it is due to the the value you get for "g_form.getValue('u_contract_value').substring(4);" i.e., 1,000 and "," comes under octal literal which should not be used in the method parseFloat(). By having that "," in the value "1,000" system takes the integer value as "1" and gives the calculated output for "1". Hence you need to replace all the commas in the "g_form.getValue('u_contract_value').substring(4);"(which is currency field) and then parseFloat it to get the actual/correct output.

Solution is:

var date = g_form.getValue('u_over_due_date');

alert(g_form.getValue('u_contract_value')); 

var value = g_form.getValue('u_contract_value'); 

var value1 = g_form.getValue('u_contract_value').substring(4);//You will get 1,000 from this code

var value_final =value1.replace(/,/g, "");//Replaces all commas in "value1"

var person = g_form.getValue('u_penalty_percentage');

var total = parseFloat(date) * parseFloat(value_final) * parseFloat(person);

 

alert(total); //Gives correct output But not NaN

 

Can you also mark the answer as correct and helpful to help future searches.

Regards,

Lini Aadhi