Field value changed by [onchnage client script] do not be saved

ESL
ServiceNow Employee
ServiceNow Employee

Hi,

I have some client script to calculate value.
The script was run correctly but when I save the form, the total number was not correct.

The value in one filed was not saved. 
I tried to recreated the client script, some time it fixed, but another script changed to failed again.
Any I deal to fix this issue???

 

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

    var h1 = g_form.getDecimalValue('u_value1');
    if (h1 === '') {
        return;
    }
	
    var h2 = g_form.getDecimalValue('u_value2');
    if (h2 === '') {
        return;
    }
	
    var newValue2 = newValue.replace(/,/g,'');
	
    var total = parseFloat(newValue2) + parseFloat(h2);	
    var totalhour = total.toFixed(2).toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");

    g_form.setValue('u_hours_total', totalhour + '');

//Type appropriate comment here, and begin script below

}
1 ACCEPTED SOLUTION

ESL
ServiceNow Employee
ServiceNow Employee

Hi found the issue of my code.

When the record is saved and the form is reloaded, the onChange client scripts should not be running, but they are running to set the newValue to 0 and then calculate the total.

Here is the root cause:

    if (isLoading || newValue === '') {
        newValue ='0,0';
    }

 

Solution: Change the code as below

if (isLoading) {
    return;
}

if (newValue === '') {
     newValue = '0,0';
}

View solution in original post

8 REPLIES 8

ESL
ServiceNow Employee
ServiceNow Employee

Hi Ozawa -san

Thanks for comment.

 

I can show you the situation. Actually I have 7 fields for input and 1 field for showing the result.

1st field is the calculate result field(Total)

2nd field ~ 8th field you can input any number.

For example, When I input 1+2+3+4+5+6+70 , the client script worked as expected and the result was correct, it shows 91. 

find_real_file.png

But after I saved the form, you can see the total changed to 21, the value in 8th field was not calculated.

find_real_file.png

 

I tried to deleted the client script for 8th field and create a same client script for it. It looks ok in the first time, but after that same issue happened on another field ( 2nd field or 4th field). 

I have no idea what is the root cause for this one.

Thanks,

Hi,

So the situation is as below.

  1. there is a custom table with fields 1 to 8 of type decimal
  2. there is a client script on field 8 that would sum fields 2 to 8 and set the total in field 1.
  3. When the values are entered in the form, field 1 contains the correct sum of fields 2 to 8
  4. Form is "Saved" and is selected from the list view. Value of field 1 is changed to a different value

Is the above situation true?

1. I've created a table with fields1 to 8 that have type Decimal.

find_real_file.png

2. Create onChange client script on variable Field8

function onChange(control, oldValue, newValue, isLoading, isTemplate) {
    if (isLoading || newValue === '') {
        return;
    }
    var fieldNames = ['u_field2', 'u_field3', 'u_field4', 'u_field5', 'u_field6', 'u_field7'];
    var sum = 0;
    for (var i = 0; i < fieldNames.length; i++) {
        sum += Number(g_form.getValue(fieldNames[i]));
    }
    sum += Number(newValue);
    g_form.setValue('u_field1', sum.toFixed(2));
}

find_real_file.png

3. Open a form and enter values in fields 2 through 8.

find_real_file.png

4. Click on "Submit" button to save fields. Fields and values are shown on the list and are correct.

find_real_file.png

5. Click on created record to re-open the record. The values are correct.

find_real_file.png

ESL
ServiceNow Employee
ServiceNow Employee

Hi found the issue of my code.

When the record is saved and the form is reloaded, the onChange client scripts should not be running, but they are running to set the newValue to 0 and then calculate the total.

Here is the root cause:

    if (isLoading || newValue === '') {
        newValue ='0,0';
    }

 

Solution: Change the code as below

if (isLoading) {
    return;
}

if (newValue === '') {
     newValue = '0,0';
}