Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

@davilu 

 

Since you mentioned that it is working well at the native view and the issue is on the portal?

Can you please hide Save (Ctrl + S) UI action and expose the "Save" UI action? 

 

https://www.servicenow.com/community/itsm-forum/how-to-hide-save-ctrl-s-button-from-itil-users-on-th...

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.