g_form.submit() is not working with spModal PoupUp Window

Pritam Khude
Tera Contributor
Hello Community,

I have a Catalog Client Script (onSubmit) running in Service Portal.
When a user selects Yes on a variable and no attachment is present, I display an spModal with two buttons:

  • Okay → stay on the form (do not submit)
  • Cancel → submit the request

I call g_form.submit() when the user clicks Cancel. However, that triggers my onSubmit again, the same condition is met, the modal opens again, and I end up in a submission loop. and form not getting submit.



function
onSubmit() {

    var errorAppear = g_form.getValue('gw_mi_did_any_errors_appear');

    if (errorAppear === 'Yes' && (this.document.getElementsByClassName('get-attachment').length == 0)) {
      spModal.open({
            title: 'Attachment recommended',
            message: 'You mentioned an error occurred, if possible please attach a screenshot of the error',

            buttons: [{
                    label: 'Okay',
                    primary: true
                },
                {
                    label: 'Cancel',
                    primary: true
                }
            ]
        }).then(function(button) {
            if (button.label === 'Cancel') {
                g_form.addInfoMessage("Submit");
                g_form.submit();
                return true;
            }
        });

        return false; // Block original submit until user choose
    }
    return true; // Allow normal submit if condition not met
}
10 REPLIES 10

@Pritam Khude 

the modal is shown since file is missing.

if user clicks Cancel and file is not there then the validation is bypassed

what's the purpose then to show modal and ask user to add file?

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

adityahubli
Tera Guru

Hello @Pritam Khude ,

This issue occurs because calling g_form.submit() inside an onSubmit Catalog Client Script triggers the onSubmit function again. Since the same condition is still true, the script opens the spModal repeatedly, causing a recursive submission loop and preventing the form from being submitted.

Additionally, returning true inside the spModal.then() callback does not affect the original onSubmit execution, because spModal.open() is asynchronous.

Another issue is that the spModal button configuration does not include a value attribute, which makes it harder to reliably determine which button the user clicked.

To resolve this, we must:

  • Use a flag variable to bypass validation during re-submit

  • Use button values in spModal

  • Avoid relying on return true inside the modal callback

 

var allowSubmit = false;

function onSubmit() {

    if (allowSubmit) {
        return true; 
    }

    var errorAppear = g_form.getValue('gw_mi_did_any_errors_appear');
    var hasAttachment = document.getElementsByClassName('get-attachment').length > 0;

    if (errorAppear === 'Yes' && !hasAttachment) {

        var opt = {
            title: 'Attachment recommended',
            message: 'You mentioned an error occurred. If possible, please attach a screenshot.',
            buttons: [
                {
                    label: 'Okay',
                    value: 'okay',
                    primary: true
                },
                {
                    label: 'Cancel',
                    value: 'cancel'
                }
            ]
        };

        spModal.open(opt).then(function (result) {
            if (result.value === 'cancel') {
                allowSubmit = true; 
                g_form.submit();    
            }
        });

        return false; // stop original submit
    }

    return true;
}

 

 

If this helps you then mark it as helpful and accept as solution.

Regards,

Aditya,

Technical Consultant

@adityahubli 

Would you mind sharing a small gif/video to showcase your above script is working fine?

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

Hello @Ankur Bawiskar ,

I did not execute this code directly. I only dry-ran Pritam’s code and, based on a similar scenario I faced earlier with a recursive submit issue, I introduced a flag variable as a preventive measure. I also noticed that Pritam missed the value attribute in the button syntax.

@adityahubli 

It would be great if we could provide a working solution so the requester doesn’t have to keep trying different approaches. This will make our support more effective and reduce additional questions

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