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

No worries!  It's just the Form widget on the portal.

Ankur Bawiskar
Tera Patron
Tera Patron

@davilu 

you are not saving that value 100 in that field in your onSubmit

try this

function onSubmit() {
    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', 100);
    // give some timeout if required

    var setTimer = '';
    setTimer = setTimeout(setTimerDelay, 5000); /*to set the timer for 5 sec*/
    function setTimerDelay() {
      return true; // alllow form submission
    }
}

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thanks @Ankur Bawiskar but it still doesn't work.  The total still clears out and does not get saved.  

@davilu 

did you try to disable the onchange client script 1 by 1?

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

I just tried that and still same results.  This is my modified script per your suggestion:

function onSubmit() {
    // initialize vars to capture weights
    var setTimer = '';
    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;
    } else {
        g_form.setValue('critical_element_total_weight', 100);
        setTimer = setTimeout(setTimerDelay, 5000);

    }

    /*to set the timer for 5 sec*/
    function setTimerDelay() {
        alert(g_form.getValue('critical_element_total_weight'))
        return true;
    }
}

When I press save, the same result happens, but the order is a bit weird.  First the Total column clears out and then the "Record Updated" info message appears, then about 2 seconds later my alert showing 100 appears.  It doesn't seem like the setValue and return true is following the 5 sec delay.