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 ,

First thing please deactivate onSubmit Client script.
then update the onChange Client script as below-

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

    // Check if the state has changed to "Qualified"
    if (g_form.getValue('state') === 'Qualified') {
        var zeroFieldValues = mandatoryFieldValueLabels();
        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() {
    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 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,

 

I have deactivated the onSubmit Script and used the script above but unfortunately it still just goes to qualified without giving me the little pop up warning.

 

patrykprejs_0-1717581299678.png

 

Thanks,

Patryk

Community Alums
Not applicable

Hi @patrykprejs ,
I think there is some issue with the script.

Can you try the script again, this has the logs.

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

    console.log('State changed to: ' + newValue);

    // Check if the state has changed to "Qualified"
    if (newValue === 'Qualified') { // Replace 'Qualified' with the actual internal value if necessary
        console.log('State is now Qualified');
        var zeroFieldValues = mandatoryFieldValueLabels();
        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
                console.log('State change rolled back');
            }
        }
    }
}

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();
}


Please ensure the field names are proper double check it.

and check the logs.

 

Thanks,

Sanjay Kumar