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

Ankur Bawiskar
Tera Patron

@Pritam Khude 

this worked for me, you can also enhance

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',
                value: 'agree',
                primary: true
            }, {
                label: 'Cancel',
                value: 'disagree'
            }]
        }).then(function(result) {
            // Both buttons clicked → NO submission
            // User must click Submit button again
            return false;
        });

        return false; // CRITICAL: Stop form submission immediately
    }

    // Only reaches here if attachment exists OR number != 'Yes'
    return true;
}

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Pritam Khude 

The script I shared is working fine and Output below

sp modal logic working.gif

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

@Pritam Khude 

Hope you are doing good.

Did my reply answer your question?

💡 If my response helped, please mark it as correct and close the thread 🔒— this helps future readers find the solution faster! 🙏

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

Hello @Ankur Bawiskar ,

Thank you for your assistance. The code you provided displays the spModal correctly. However, when the user clicks the Cancel button, the form does not get submitted—it goes into a loop again if I use g_form.submit(); under this code.

 

 }).then(function(result) {
           if (result.label === 'Cancel') {
     g_form.submit();
  }
 return false;
I also tried using your code as-is. When the user clicks the Cancel button, the spModal closes as expected. However, if the user clicks the Submit button again to submit the request, the spModal is triggered again, and the form does not get submitted.
I want to submit the form if user click on cancel button. 
 
 
Thank you.