Stop Form Submission if User Selects "Cancel" in a Confirm Pop-Up Box in Client Script

paatmarquez
Tera Contributor

Hello,

 

We have a setup in our incident form wherein every time the Service Offering field is populated by the user, the Assignment Group is auto-populated by the Support Group tied with the Service Offering. Now, we allow users to select an assignment group that is different from the tied support group of the selected Service Offering. 

 

The requirement is, we need to prompt and alert the users upon submission when the assignment group they selected is different from the support group of the selected Service Offering. If they click "Ok", the form should submit successfully. If they clicked "Cancelled", the form should not submit.

 

Everything seemed to be working fine except for one piece: I cannot seem to stop the form submission if "Cancelled" is selected.

 

I've made a Script include and Client Script to achieve this. Could someone help me cross-check it?

 

Client Script:

function onSubmit() {
    //Type appropriate comment here, and begin script below

    //Type appropriate comment here, and begin script below
    var ServiceOff = g_form.getValue('service_offering');

    //Call script include
    var ga = new GlideAjax('global.IncidentAssignmentGroup'); //Scriptinclude
    ga.addParam('sysparm_name', 'getServOff'); //Method
    ga.addParam('servOffer', ServiceOff); //Parameters
    ga.getXMLAnswer(getResponse);
   
    function getResponse(response) {
        var res = JSON.parse(response);
        // Cross-checks the selected assignment group against the current support group of the selected Service Offering
        if (g_form.getValue('assignment_group') != res.support_group || !g_form.getValue('assignment_group')) {
            var answer = confirm("The selected assignment group is not associated with the Service Offering. Would you like to proceed?");
			if(answer==true){
                return true;
            }
			else {
				return false;
			}
        }
    }
}

 

Script Include:

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

    getServOff: function() {

        var servOffer = this.getParameter('servOffer');
        obj = {};
		
		var grServOffer = new GlideRecord('service_offering');
        if (grServOffer.get(servOffer)) {
            obj.support_group = grServOffer.getValue('support_group');
			obj.parent = grServOffer.getValue('parent'); 
        }
        return JSON.stringify(obj);
    },

    type: 'IncidentAssignmentGroup'
});

 

Any help is appreciated. Thanks!

4 REPLIES 4

Ankur Bawiskar
Tera Patron
Tera Patron

@paatmarquez 

you are using asynchronous GlideAjax so by the time the script include returns the value the form will get submitted.

Why not use before insert/update BR?

If not then check this link on how to achieve this using GlideAjax + onSubmit and make it synchronous

How To: Async GlideAjax in an onSubmit script 

If my response helped please mark it correct and close the thread so that it benefits future readers.

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

Hi @Ankur Bawiskar. Thanks for your response. I tried updating my OnSubmit Client Script but unfortunately, its still not working. 

 

Here's the updated client script:

function onSubmit() {

    if (g_scratchpad._ajaxChecked) {
        g_scratchpad._ajaxChecked = null;
        return true;
    }

    var ServiceOff = g_form.getValue('service_offering');

    //Call script include
    g_scratchpad._ajaxChecked = false;
    var ga = new GlideAjax('global.IncidentAssignmentGroup'); //Scriptinclude
    ga.addParam('sysparm_name', 'getServOff'); //Method
    ga.addParam('servOffer', ServiceOff); //Parameters
    ga.getXMLAnswer(getResponse);

    function getResponse(response) {
        var res = JSON.parse(response);
        // Cross-checks the selected assignment group against the current support group of the selected Service Offering
        if (g_form.getValue('assignment_group') != res.support_group || !g_form.getValue('assignment_group')) {
            var answer = confirm("The selected assignment group is not associated with the Service Offering. Would you like to proceed?");
            if (answer != true) {
                return false;
            }
        }
return;
    }
	g_scratchpad._ajaxChecked = true;
}

 

@paatmarquez 

Did you debug the client script by adding alert?

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

Hi @Ankur Bawiskar 

 

I was now able to find solution for this. Here is the updated script:

function onSubmit() {

    if (g_scratchpad.isFormValid) {
        return true;
    }

    var ServiceOff = g_form.getValue('service_offering');

    //Call script include
    var ga = new GlideAjax('global.IncidentAssignmentGroup'); //Scriptinclude
    ga.addParam('sysparm_name', 'getServOff'); //Method
    ga.addParam('servOffer', ServiceOff); //Parameters
    ga.getXMLAnswer(getResponse);

    function getResponse(response) {
        var res = JSON.parse(response);
        // Cross-checks the selected assignment group against the current support group of the selected Service Offering
        if (g_form.getValue('assignment_group') != res.support_group || !g_form.getValue('assignment_group')) {
            var answer = confirm("The selected assignment group is not associated with the Service Offering. Would you like to proceed?");
            if (answer != true) {
                return false;
            }
        }
        var actionName = g_form.getActionName();
        g_scratchpad.isFormValid = true;
        g_form.submit(actionName);
    }

    return false;
}

 

Thanks for your help!