onSubmit client script clearing out total column

davilu
Mega Sage

Our team created a form that has 5 integer columns that are supposed to add up to 100 and display in another integer column that displays that total.  

 

Each of the 5 individual integer columns have the following onChange client script:

 

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

    //Type appropriate comment here, and begin script below
    var minValue = 25; //g_form.getValue('critical_element_1_min_weight');
    //alert(minValue)
	var m1, m2, m3, m4, m5;
    if (newValue) {
        m1 = parseInt(newValue);
    } else {
		m1 = 0;
	}
    if (g_form.getValue('critical_element_2_weight')) {
        m2 = parseInt(g_form.getValue('critical_element_2_weight'));
    }else {
		m2 = 0;
	}
    if (g_form.getValue('critical_element_3_weight')) {
        m3 = parseInt(g_form.getValue('critical_element_3_weight'));
    }else {
		m3 = 0;
	}
    if (g_form.getValue('critical_element_4_weight')) {
        m4 = parseInt(g_form.getValue('critical_element_4_weight'));
    }else {
		m4 = 0;
	}
    if (g_form.getValue('critical_element_5_weight')) {
        m5 = parseInt(g_form.getValue('critical_element_5_weight'));
    }else {
		m5 = 0;
	}

    if (oldValue != newValue) {
        if (parseInt(newValue) < minValue) {
            g_form.clearValue('critical_element_1_weight');
            g_form.showFieldMsg('critical_element_1_weight', 'Please input a value greater than or equal to the minimum value of ' + minValue, 'error');
        }
    }
	g_form.setValue('critical_element_total_weight', m1 + m2 + m3 + m4 + m5);
}

 

The total integer column has the following onSubmit client script:

function onSubmit() {
    // initialize vars to capture weights
    var weight1 = parseInt(g_form.getValue('critical_element_1_weight'));
    var weight2 = parseInt(g_form.getValue('critical_element_2_weight'));
    var weight3 = parseInt(g_form.getValue('critical_element_3_weight'));
    var weight4 = parseInt(g_form.getValue('critical_element_4_weight'));
    var weight5 = parseInt(g_form.getValue('critical_element_5_weight'));

    var sumOfWeights = weight1 + weight2 + weight3 + weight4 + weight5;

    if (sumOfWeights !== 100) {
        alert('Total assigned weights for critical elements must equal 100.');
        return false;
    }
	alert(g_form.getValue('critical_element_total_weight'))
}

When the total equals 100 and we press submit, the alert pops up showing 100 and the column has the value of 100:

davilu_0-1746653825969.png

However, once we clear the alert, we get the "Record Updated" info message, and the column value disappears.  It also does not get saved in the back-end table.  We've checked to see if there are any other scripts interfering with this and there aren't any.  Any ideas why this is happening?

1 ACCEPTED SOLUTION

davilu
Mega Sage

Hi All, thanks for the help!  Turns out the onSubmit client script wasn't saving the value.  I ended up creating a script include that updates the value onSubmit.  It is working now.

View solution in original post

16 REPLIES 16

J Siva
Tera Sage

Hi @davilu 
As per your requirement, there are 5 fields where the minimum value should be 25. Your form will only be submitted if the total is 100. Hypothetically, 5*25 equals 125, so your form will not be submitted. Are those optional variables? If yes, then you can follow the below approach.
Instead of multiple On change client scripts, use a variable regex to check if the value is greater than or equal to 25 like below and use that in the  variables. Total will be calculated using the On submit client script.

JSiva_0-1746675347984.png

On Submit client script:

function onSubmit() {
    var weight1 = parseInt(g_form.getValue('input_1'));
    var weight2 = parseInt(g_form.getValue('input_2'));
    var weight3 = parseInt(g_form.getValue('input_3'));
    var weight4 = parseInt(g_form.getValue('input_4'));
    var weight5 = parseInt(g_form.getValue('input_5'));

    var sumOfWeights = weight1 + weight2 + weight3 + weight4 + weight5;

    if (sumOfWeights !== 100) {
        alert('Total assigned weights for critical elements must equal 100.');
        return false;
    }
	var total_weight = g_form.setValue('total',sumOfWeights);
    alert(sumOfWeights);

}

Output:

JSiva_1-1746675453970.png


Regards,
Siva

Hi @J Siva, how do you get to that regex definition screen?

@davilu please check the below product doc to configure to the variable validation regex.

https://www.servicenow.com/docs/bundle/yokohama-servicenow-platform/page/product/service-catalog-man...

oh gotcha, this is actually on the Form and not Record Producer, so I don't think there's a regex option.

My bad. I thought it's catalog item. May I know what's that UI? Is that the workspace?