The CreatorCon Call for Content is officially open! Get started here.

onsubmit client script help - submitting when it shouldn't

dave_edgar
Mega Guru

I have this script and although i get the alert/infomessage it is still submitting

 

 

 

function onSubmit() {
    var dateTimeString = g_form.getValue('date');
    //	console.log("Check count of Requests - dateTimeString = " + dateTimeString);
    var dateOnly = dateTimeString.split(' ')[0];
    //console.log("Check count of Requests - dateOnly = " + dateOnly);

    var ga = new GlideAjax('CountCheck');
    ga.addParam('sysparm_name', 'getCount');
    ga.addParam('sysparm_date', dateOnly);
    ga.getXML(reply);

    function reply(response) {

        var answer = response.responseXML.documentElement.getAttribute("answer");
        console.log("Check count of Requests - answer = " + answer);
        console.log("Check count of Requests - dateOnly = " + dateOnly);

        if ((dateOnly == '24/08/2023' || dateOnly == '25/08/2023' || dateOnly == '26/08/2023' || dateOnly == '27/08/2023' || dateOnly == '28/08/2023') && answer < 2) {
            alert("Date available and your request has been submitted for" + dateOnly + ".  The process will action on the chosen date, we will endevaour to email you a reminder prior to the changes being implemented.");
            return true;
        } 
		else if ((dateOnly != '24/08/2023' || dateOnly != '25/08/2023' || dateOnly != '26/08/2023' || dateOnly != '27/08/2023' || dateOnly != '28/08/2023') && answer >= 2) {
return false;
 g_form.addInfoMessage("Date "+ dateOnly + " not available. Select a different date.");
			alert("Date "+ dateOnly + " not available. Select a different date.");
        }
		else {
return false;
    }
	}
}

 

 

3 REPLIES 3

Nayan  Dhamane
Kilo Sage

Hello @dave_edgar ,

 

Try getXMLWait() instead of getXML().

 

If my answer solved your issue, please mark my answer as Correct & Helpful based on the Impact

Best Regards,
Nayan Dhamane
ServiceNow Community Rising Star 2023.

Sandeep Rajput
Tera Patron
Tera Patron

@dave_edgar Since you are making an asynchronous GlideAjax call, hence the form is getting submitted despite the alert being shown. 

 

Please refer to this support article https://support.servicenow.com/kb?id=kb_article_view&sysparm_article=KB0783579 where the script initially returns false to stop the form submission and returns true/false once the GlideAjax response is received from the server.

 

Hope this helps.

Tai Vu
Kilo Patron
Kilo Patron

Hey @dave_edgar 

The form is still submitted due to the Asynchronous GlideAjax callThe form is submitted before the response arrives, causing your validations to not execute.

 

To address this, your script should look like the example below.

function onSubmit() {
    var dateTimeString = g_form.getValue('date');
    var dateOnly = dateTimeString.split(' ')[0];

	/*** Pop this gem into your script! */
    if (g_scratchpad.isFormValid) {
        return true;
    }
    var actionName = g_form.getActionName();

    var ga = new GlideAjax('CountCheck');
    ga.addParam('sysparm_name', 'getCount');
    ga.addParam('sysparm_date', dateOnly);
    ga.getXML(reply);

    function reply(response) {
        var answer = response.responseXML.documentElement.getAttribute("answer");
        if ((dateOnly == '24/08/2023' || dateOnly == '25/08/2023' || dateOnly == '26/08/2023' || dateOnly == '27/08/2023' || dateOnly == '28/08/2023') && answer < 2) {
            alert("Date available and your request has been submitted for" + dateOnly + ".  The process will action on the chosen date, we will endevaour to email you a reminder prior to the changes being implemented.");
			
			/*** Pop this gem into your script! */
            g_scratchpad.isFormValid = true;
            g_form.submit(actionName);
        } else if ((dateOnly != '24/08/2023' || dateOnly != '25/08/2023' || dateOnly != '26/08/2023' || dateOnly != '27/08/2023' || dateOnly != '28/08/2023') && answer >= 2) {
            g_form.addInfoMessage("Date " + dateOnly + " not available. Select a different date.");
            alert("Date " + dateOnly + " not available. Select a different date.");
            return false;
        }
        return false;
    }
    return false;
}

 

 

KB0779964.png

Check out this KB reference: #KB0779964

 

Cheers

Tai Vu

Feel free to give that thumbs-up button a little tap dance if the response deserves a gold star! 🌟