confirm() not working onSubmit Client Script

Jackhol
Tera Contributor

Hi all!

 

I am trying to use confirm() on a onSubmit Client Script and I am running into some problems.

 

What I am trying to achieve is, Incidents logged to a certain Assignment Group will prompt the analyst if there is no attachment associated with the record.

The GlideAjax for the attachment check works, and the confirmation box pops when save is clicked. However, regardless of what option is selected the form saves. 

 

I have created a client script:

 

function onSubmit() {
    var assGroup = g_form.getValue('assignment_group');
    if (assGroup == 'a60fe98cdb27d85006d43ce3399619d7') {

        var ga = new GlideAjax('check_open_inc');
        ga.addParam('sysparm_name', 'checkAttach');
        ga.addParam('sysparm_ticket', g_form.getUniqueValue()); //Sending Ticket sys_id as parameter.
        ga.getXMLAnswer(attachCheck);

    }

    function attachCheck(response) {
        if (response == 0) {
            var conf = confirm("You have not added an Attachment to this ticket, click Cancel to add an attachment, click Ok to continue");
            return conf;
        }

    }
 
I have created the glideAjax:
 
checkAttach: function(){
            var ticket = this.getParameter("sysparm_ticket");
            var attachments = new GlideRecord('sys_attachment');
            attachments.addQuery('table_sys_id',ticket);
            attachments.query();
            var rowCount =   attachments.getRowCount();
            return JSON.stringify(rowCount);
        },
 
I have tried several different combinations of "return" and none of them seem to work. It appears that the form is already saving before an option is selected from the confirmation. 
 
Thank you
1 ACCEPTED SOLUTION

Hi @Jackhol ,

 

I did configure this logic in my PDI it works 

Client script :

function onSubmit() {
   //Type appropriate comment here, and begin script below
   var assGroup = g_form.getValue('assignment_group');

    if (assGroup == '477a05d153013010b846ddeeff7b1225') {  //i have tried some sample sys_id here

        var ga = new GlideAjax('check_open_inc');
        ga.addParam('sysparm_name', 'checkAttach');
        ga.addParam('sysparm_ticket', g_form.getUniqueValue()); //Sending Ticket sys_id as parameter.
        ga.getXMLWait();
		var answer = ga.getAnswer();
		if (answer) {
            var conf = confirm("You have not added an Attachment to this ticket, click Cancel to add an attachment, click Ok to continue");

    }
}
}

 

Script include : I am just returning true from script include to test confirm before form submit

var check_open_inc = Class.create();
check_open_inc.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkAttach: function(){
 var ticket = this.getParameter("sysparm_ticket");
 return true; //i am returning true to test confirm
},
    type: 'check_open_inc'
});

 Result :

HemanthM1_0-1698072936204.png

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

View solution in original post

8 REPLIES 8

Hemanth M1
Giga Sage
Giga Sage

Hi @Jackhol ,

 

Since its OnSubmit getXMLAnswer() wouldn't work here since its Async form will be submitted before response received from the script include

 

use getXMLWait()  instead (FYI: this wouldn't work if use this in Service portal)

ga.getXMLWait();

answer = ga.getAnswer();

if(

**********
unction onSubmit() {
    var assGroup = g_form.getValue('assignment_group');
    if (assGroup == 'a60fe98cdb27d85006d43ce3399619d7') {

        var ga = new GlideAjax('check_open_inc');
        ga.addParam('sysparm_name', 'checkAttach');
        ga.addParam('sysparm_ticket', g_form.getUniqueValue()); //Sending Ticket sys_id as parameter.
        ga.getXMLWait(); 
answer = ga.getAnswer();
        if (answer == 0) {
            var conf = confirm("You have not added an Attachment to this ticket, click Cancel to add an attachment, click Ok to continue");
            return conf;
        }
   
    }

   

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Hi Hemanth,

 

I have tried your script this morning, unfortunately the INC just saves and the Confirmation Box does not appear. 

 

Thank you

Hi @Jackhol ,

 

I did configure this logic in my PDI it works 

Client script :

function onSubmit() {
   //Type appropriate comment here, and begin script below
   var assGroup = g_form.getValue('assignment_group');

    if (assGroup == '477a05d153013010b846ddeeff7b1225') {  //i have tried some sample sys_id here

        var ga = new GlideAjax('check_open_inc');
        ga.addParam('sysparm_name', 'checkAttach');
        ga.addParam('sysparm_ticket', g_form.getUniqueValue()); //Sending Ticket sys_id as parameter.
        ga.getXMLWait();
		var answer = ga.getAnswer();
		if (answer) {
            var conf = confirm("You have not added an Attachment to this ticket, click Cancel to add an attachment, click Ok to continue");

    }
}
}

 

Script include : I am just returning true from script include to test confirm before form submit

var check_open_inc = Class.create();
check_open_inc.prototype = Object.extendsObject(AbstractAjaxProcessor, {

checkAttach: function(){
 var ticket = this.getParameter("sysparm_ticket");
 return true; //i am returning true to test confirm
},
    type: 'check_open_inc'
});

 Result :

HemanthM1_0-1698072936204.png

 

 

Accept and hit Helpful if it helps.

Thank you,
Hemanth
Certified Technical Architect (CTA), ServiceNow MVP 2024, 2025

Thank you that worked!