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

folusho
Tera Guru

@davilu 

 

Please try this for your onSubmit client script.

 

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

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

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

    g_form.setValue('critical_element_total_weight', sumOfWeights); // Force the value to be saved

    return true;
}

 

Hi @folusho, thanks for your suggestion.  I tried that but still the same result.

davilu
Mega Sage

One thing to add is if we do this in the back-end platform view and press Save, the total gets saved and it also appears in the portal:

 

Platform view:

davilu_0-1746666071775.png

Portal view:

davilu_1-1746666119271.png

I'm not sure if there's something with the UI Action on the portal, but it is OOTB so it should work...

 

@davilu 

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...