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.

Change onSubmit Client Script to onChange

patrykprejs
Tera Contributor

Hi All,

 

I have got a working onSubmit Script for the Demand table, essentially when a few specified fields are left with a zero value and the Demand is submitted a pop up appears, sort of like a warning to make you aware.

 

patrykprejs_0-1717493081990.png

 

I now need this to also trigger when the Demand moves into the "Qualified" State, I have tried a few things but am not able to get it working and I assumed that the easiest option would be to use a separate onChange script but as mentioned before had no luck with it, below is the onSubmit script currently in use:

patrykprejs_2-1717493358014.png

 

function onSubmit() {
    function mandatoryFieldValueLabels() {
        var labels = '';
        var fieldNames = ['financial_benefit','capital_outlay','operational_outlay','u_external_cost'];
        var emptyFields = fieldNames.filter(function(field) {
            return g_form.getIntValue(field) <= 0;
        });
        var fieldLabels = emptyFields.map(function(fieldName) {
            g_form.hideFieldMsg(fieldName);
            g_form.showFieldMsg(fieldName, 'Check valid zero value', 'error');
            return g_form.getLabelOf(fieldName);
        });
        return fieldLabels.join('\n').trim();
    }
    if (g_form.getActionName() == 'sysverb_insert') { // Was 'qualify' changed with STRY0018402
        var zeroFieldValues = mandatoryFieldValueLabels();
        if (zeroFieldValues.length > 0) {
            var result = confirm('The following finance fields have a zero value, please confirm you wish to proceed: \n\n' + mandatoryFieldValueLabels());
            if (!result) {
                return false;
            }
        }
    }
}

 

 

Any help would be greatly appreciated.

 

Thank you.

 

7 REPLIES 7

Community Alums
Not applicable

Hi @patrykprejs ,

 

You should definitely have two separate scripts, one for OnChange and other for OnSubmit.

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

    if (g_form.getValue('state') === 'Qualified') {
        var zeroFieldValues = mandatoryFieldValueLabels(); // Reuse your existing function
        if (zeroFieldValues.length > 0) {
            var result = confirm('The following finance fields have a zero value. Do you wish to proceed?\n\n' + zeroFieldValues);
            if (!result) {
                g_form.setValue('state', oldValue); // Roll back the state change
            }
        }
    }
}

function mandatoryFieldValueLabels() {
    // Your existing logic here
    // ...
}

 

If my response has resolved your query, please consider giving it a thumbs up ‌‌ and marking it as the correct answer‌‌!

Thanks & Regards,

 

Sanjay Kumar

Hi @Community Alums,

 

Thank you for the above suggestion, when I tried to implement it is still not doing anything. Am I missing something?

(Apologies, I am still learning on the scripting aspect of my knowledge)

patrykprejs_0-1717509920828.png

 

 

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

    if (g_form.getValue('state') === 'qualified') {
        var zeroFieldValues = mandatoryFieldValueLabels(); // Reuse your existing function
        if (zeroFieldValues.length > 0) {
            var result = confirm('The following finance fields have a zero value. Do you wish to proceed?\n\n' + zeroFieldValues);
            if (!result) {
                g_form.setValue('state', oldValue); // Roll back the state change
            }
        }
    }
}

function mandatoryFieldValueLabels() {
    // Your existing logic here
    // ...
	var labels = '';
        var fieldNames = ['financial_benefit','capital_outlay','operational_outlay','u_external_cost'];
        var emptyFields = fieldNames.filter(function(field) {
            return g_form.getIntValue(field) <= 0;
        });
        var fieldLabels = emptyFields.map(function(fieldName) {
            g_form.hideFieldMsg(fieldName);
            g_form.showFieldMsg(fieldName, 'Check valid zero value', 'error');
            return g_form.getLabelOf(fieldName);
        });
    
}

 

 

 

Community Alums
Not applicable

Hi @patrykprejs ,
can you please share both the client scripts so that I can take a look?

 

Thanks,

Sanjay kumar

 

Hi @Community Alums,

 

Of course, thank you for looking over this both scripts are below:

 

onSubmit Script:

 

function onSubmit() {
    function mandatoryFieldValueLabels() {
        var labels = '';
        var fieldNames = ['financial_benefit','capital_outlay','operational_outlay','u_external_cost'];
        var emptyFields = fieldNames.filter(function(field) {
            return g_form.getIntValue(field) <= 0;
        });
        var fieldLabels = emptyFields.map(function(fieldName) {
            g_form.hideFieldMsg(fieldName);
            g_form.showFieldMsg(fieldName, 'Check valid zero value', 'error');
            return g_form.getLabelOf(fieldName);
        });
        return fieldLabels.join('\n').trim();
    }
    if (g_form.getActionName() == 'sysverb_insert') { // Was 'qualify' changed with STRY0018402
        var zeroFieldValues = mandatoryFieldValueLabels();
        if (zeroFieldValues.length > 0) {
            var result = confirm('The following finance fields have a zero value, please confirm you wish to proceed: \n\n' + mandatoryFieldValueLabels());
            if (!result) {
                return false;
            }
        }
    }
}

 

 

onChange Script:

 

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

    if (g_form.getValue('state') === 'qualified') {
        var zeroFieldValues = mandatoryFieldValueLabels(); // Reuse your existing function
        if (zeroFieldValues.length > 0) {
            var result = confirm('The following finance fields have a zero value. Do you wish to proceed?\n\n' + zeroFieldValues);
            if (!result) {
                g_form.setValue('state', oldValue); // Roll back the state change
            }
        }
    }
}

function mandatoryFieldValueLabels() {
    // Your existing logic here
    // ...
        var labels = '';
        var fieldNames = ['financial_benefit','capital_outlay','operational_outlay','u_external_cost'];
        var emptyFields = fieldNames.filter(function(field) {
            return g_form.getIntValue(field) <= 0;
        });
        var fieldLabels = emptyFields.map(function(fieldName) {
            g_form.hideFieldMsg(fieldName);
            g_form.showFieldMsg(fieldName, 'Check valid zero value', 'error');
            return g_form.getLabelOf(fieldName);
        });
        return fieldLabels.join('\n').trim();
    }

 

 

Thank you.