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

Community Alums
Not applicable

Hello.

Could you please check if another client script or UI policy working on same field.

 

Regards,

Akshay

ESL
ServiceNow Employee
ServiceNow Employee

I have checked I don not have another UI policy / client script on the same filed.

Client script run as well but after I save the form, the data was not correct.

Hitoshi Ozawa
Giga Sage
Giga Sage

Hi,

Would appreciate some more information. What is the script suppose to do? Which variable is onChange set to?

Hi again,

Looking at the script, it seems to be adding values of u_value1 and u_value2 fields and setting u_hours_total field. The onChange script seems to be set of variable u_value1. Is there a similar script on u_value2 field?

>The value in one filed was not saved. 

Exactly, which field? u_value1, u_value2, u_hours_total?

Does this mean the value does not appear in Requested Item when the form is submitted?

I've tested the script and it is saving the values as variables in requested item.

>I tried to recreated the client script, some time it fixed, but another script changed to failed again.

So, there is another script? It's failing in another script? Please paste the another script that is failing.