Having issue with OnSubmit client script form submission.

Mukesh Singh
Tera Contributor

Hi All,
We have a requirement to stop form submission for all tickets if users selected InValid CIs. We have Affected CIs(cmdb_ci) field on task table and IT owner field on CIs.

If IT owner field is empty in that case users should not be able to submit the form.

We have written below script using OnSubmit Client script but its not working, users are able to submit the form.

Please assist.

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var ci = g_form.getReference('cmdb_ci', callBack);

    function callBack(ci) {

        var itOwner = ci.u_it_owner;

        if (itOwner == "") {

            g_form.addErrorMessage("IT owner value for selected CIs is empty, please IT owner value gets updated or select another CIs");
            return false;

        }
    }
}



1 ACCEPTED SOLUTION

Try with below script, it will work, tested on my PDI, the issue was with async call on onSubmit action. 

 

function onSubmit() {
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var actionName = g_form.getActionName();
    var ci = g_form.getReference('cmdb_ci', callBack);

    function callBack(ci) {
        var itOwner = ci.u_it_owner;
        if (itOwner == "") {
            alert(' IT Owner is empty');
            g_form.addErrorMessage("IT owner value for selected CIs is empty, please IT owner value gets updated or select another CIs");
            return false;

        }
        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);
    }




    return false;
}

 

Thanks,

Harsh

View solution in original post

4 REPLIES 4

Harsh Vardhan
Giga Patron

Hi @Mukesh Singh  - You can try to debug this by adding alert() . 

try with below script and see what are you getting in alert .

 

function onSubmit() {
    //Type appropriate comment here, and begin script below
    var ci = g_form.getReference('cmdb_ci', callBack);

    function callBack(ci) {

        var itOwner = ci.u_it_owner;
alert( ' IT Owner is ' + itOwner );
        if (itOwner == "") {
alert( ' IT Owner is empty' );
            g_form.addErrorMessage("IT owner value for selected CIs is empty, please IT owner value gets updated or select another CIs");
            return false;

        }
    }
}

 

Check what are you getting in those two alert ?

 

Thanks,

Harsh

Hi Harsh,
I have tried to change request with it owner field empty and got both alert message.
alert 1 = IT Owner is

alert 2 = IT Owner is empty
Once clicked 'Ok' on alert messages, change request got created.

Try with below script, it will work, tested on my PDI, the issue was with async call on onSubmit action. 

 

function onSubmit() {
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var actionName = g_form.getActionName();
    var ci = g_form.getReference('cmdb_ci', callBack);

    function callBack(ci) {
        var itOwner = ci.u_it_owner;
        if (itOwner == "") {
            alert(' IT Owner is empty');
            g_form.addErrorMessage("IT owner value for selected CIs is empty, please IT owner value gets updated or select another CIs");
            return false;

        }
        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);
    }




    return false;
}

 

Thanks,

Harsh

Thank you Harsh. Its working now.