Client Script for adding values from 3 fields and populating it on other field
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2017 07:09 AM
Hi,
I need some help scripting on adding values from 3 fields and populating them in a new field.
Thanks in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2017 08:57 AM
Hi Chuck,
The below is the Client Script i have written, i am getting value as NaN('u_estimated_r') after adding all 3 fields
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
var xyz=' ';
var a=parseInt(g_form.getValue('u_estimated_claim_amount_s'));
var b=parseInt(g_form.getValue('u_estimated_claim_amount_g'));
var c=parseInt(g_form.getValue('u_estimated_claim_amount_c'));
xyz +=a+b+c;
g_form.setValue('u_estimated_r',xyz);
//Type appropriate comment here, and begin script below
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-13-2017 03:15 PM
Q: What field is your onChange script monitoring? Instead of using a string to initialize your total, try this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
if (isLoading)
return;
if (newValue == oldValue)
return;
var xyz = 0;;
var a=parseInt(g_form.getValue('u_estimated_claim_amount_s'));
var b=parseInt(g_form.getValue('u_estimated_claim_amount_g'));
var c=parseInt(g_form.getValue('u_estimated_claim_amount_c'));
xyz =a + b + c;
g_form.setValue('u_estimated_r',xyz);
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2017 07:51 AM
Hi Chuck,
The below is my code.
I am getting value as 'NaN' at the total field. Can you help me on this.
function totalEstimated(){
var estsupplier = g_form.getValue('u_estimated_claim_amount_supplier');
var estgpc = g_form.getValue('u_estimated_claim_amount_gpc');
var estcarrier = g_form.getValue('u_estimated_claim_amount_carrier');
var estsupplier1 = estsupplier.match(/[^,.0-9]/g); //checking if the field contains alphabets
var estgpc1 = estgpc.match(/[^,.0-9]/g);
var estcarrier1 = estcarrier.match(/[^,.0-9]/g);
if((estsupplier1=='' || estsupplier1==null) && (estgpc1== '' || estgpc1==null) && (estcarrier1=='' || estcarrier1==null)){
var estsupplier1_1 = parseInt(g_form.getValue('u_estimated_claim_amount_supplier').replace(',', ''));
var estgpc1_1 = parseInt(g_form.getValue('u_estimated_claim_amount_gpc').replace(',', ''));
var estcarrier1_1 = parseInt(g_form.getValue('u_estimated_claim_amount_carrier').replace(',', ''));
var total_estimated = estsupplier1_1 + estgpc1_1 + estcarrier1_1;
g_form.setValue('u_estimated_recovery1' , total_estimated);
}
else if((estsupplier1!='' || estsupplier1!=null) && (estgpc1== '' || estgpc1==null) && (estcarrier1=='' || estcarrier1==null)){
alert("Only numeric characters (0-9) are allowed in 'Estimated Claim Amount - Supplier' ");
//alert('inv1_1 = '+inv1_1);
g_form.setValue('u_estimated_claim_amount_supplier', estsupplier.replace(/[^,.0-9]/g , ''));
}
else if((estsupplier1=='' || estsupplier1==null) && (estgpc1!= '' || estgpc1!=null) && (estcarrier1=='' || estcarrier1==null)){
alert("Only numeric characters (0-9) are allowed in 'Estimated Claim Amount - GPC' ");
g_form.setValue('u_estimated_claim_amount_gpc', estgpc.replace(/[^,.0-9]/g , ''));
}
else if((estsupplier1=='' || estsupplier1==null) && (estgpc1== '' || estgpc1==null) && (estcarrier1!='' || estcarrier1!=null)){
alert("Only numeric characters (0-9) are allowed in 'Estimated Claim Amount - Carrier' ");
g_form.setValue('u_estimated_claim_amount_carrier', estcarrier.replace(/[^,.0-9]/g , ''));
}
}
function onLoad() {
//Type appropriate comment here, and begin script below
var supplier = g_form.getControl('u_estimated_claim_amount_supplier');
var gpc = g_form.getControl('u_estimated_claim_amount_gpc');
var carrier = g_form.getControl('u_estimated_claim_amount_carrier');
supplier.onchange = totalEstimated;
gpc.onchange = totalEstimated;
carrier.onchange = totalEstimated;
totalEstimated();
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-14-2017 08:03 PM
Let's look at what you're seeing.
NaN means "Not a Number". So it looks like we're having an issue within your code. One common place to get a NaN is when parseInt is used on a field that has a first character which is not able to be converted.
For example, if you run this code:
var test = "$11";
gs.print (parseInt(test));
You will get NaN because of the dollar sign.
So my question is - what are the actual values in the three fields you are adding together? I would suspect that one of those parseInt's is failing. You should run each of those values through parseInt in a javascript editor (either the ServiceNow one or something like jsfiddle.net). I am willing to bet you will find one (or more) of those parseInt's giving you NaN.
(Let's just ignore the regex you're running before - I don't want to make the assumption it's running right because I have some doubt it is doing what you expect it to do)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎04-02-2021 08:12 AM
Hi Chuck,
I'm using the same script to calculate the value of a+b and get total sum value in the single line text box. This script is not working for me. can you help me
I'm using 2 select box'es
a Select Box has option yes or no (Yes value as 10 ; No Value as 5)
b Select Box has option yes or no (Yes value as 10 ; No Value as 5)
I'm using C as a single line text box I need to get the score of what the user selects.
If Select Yes in A; Yes in B;
It should autopopulate C as 10
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
var xyz ='';
var X = g_form.getValue('a');
var Y = g_form.getValue('b');
var Z = g_form.getValue('c');
xyz = X+Y+Z;
g_form.setValue('score', xyz);
return;
}
}
Please help
Thank you!!