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

Danish Bhairag2
Tera Sage
Tera Sage

Hi @Jackhol ,

 

Can u try returning false in ur client script , because of i remember correctly the output of confirm is 'yes' or 'no'. Hence it allows users to save the record.

 

After confirm on the next line just write 

 

return false;

 

Instead of return conf

 

Thanks,

Danish

 

Hi Danish, 

 

I have tried the below and i have the same outcome:

 

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

    if (assGroup = 'a60fe98cdb27d85006d43ce3399619d7') {

        var ga = new GlideAjax('check_opn_inc');
        ga.addParam('sysparm_name', 'checkAttach');
        ga.addParam('sysparm_ticket', g_form.getValue('sys_id')); //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 OK to add an attachment, click cancel to continue");
            if (conf == true) {
                return true;
            } else {
                return false;
            }
        }
    }
 
Thank you

Anand Kumar P
Giga Patron
Giga Patron

Hi @Jackhol,

Try below 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.");
if (!conf) {
// User clicked Cancel, so prevent form submition
g_form.addErrorMessage("Please add an attachment before saving.");
return false;
}
}
}
return true;
}

Thanks,

Anand

Hi Anand, 

 

Thank you for your script, unfortunately the INC just saves and no confirmation box appears. 

 

Thank you.