How do I cancel a form submission?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2017 02:48 AM
I would like to cancel the form submission in case the "if" statement below is true. how can I acheive that?? I added "return false" inside the if statement but its not working. kindly do help me out..
function onSubmit() {
//Type appropriate comment here, and begin script below
var t = g_form.getValue('u_date');
var a = new GlideAjax('testscript');
a.addParam('sysparm_name','myfunction');
a.addParam('sysusername', 'bala');
a.getXML(myback);
function myback(res)
{
var answer= res.responseXML.documentElement.getAttribute("answer");
if(t!=answer)
{
alert("current date is" + answer);
}
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2017 03:39 AM
In short (continue alikuttyka 's answer):
function onSubmit() {
//Type appropriate comment here, and begin script below
var t = g_form.getValue('u_date');
if(your condition) {
alert('some alert');
return false;
}
}
harel
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-09-2017 10:33 PM
Hi Bala,
In such cases you can use synchronous GlideAJAX and use getXMLWait() instead of getXML(). Please find the link Examples of synchronous GlideAjax . In this case if you are using getXMLWait(), you do not have to use callback function. Please find the code below:
function onSubmit() {
//Type appropriate comment here, and begin script below
var t = g_form.getValue('u_date');
var a = new GlideAjax('testscript');
a.addParam('sysparm_name','myfunction');
a.addParam('sysusername', 'bala');
a.getXMLWait();
var answer= a.getAnswer();
if(t!=answer)
{
alert("current date is" + answer);
}
}
I hope this helps.Please mark correct/helpful based on impact
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-10-2017 02:18 AM
Just a note to consider:
ServiceNow does not recommend using asynchronous getXMLWait();
In the link above, it is stated so:
If your use case demands that no further processing can occur until the GlideAjax response has been received, you can use getXMLWait(). However, because this will slow down your code and lock the user session until the response is received, it is generally recommended that you use getXML() with a callback function.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-04-2021 03:06 AM
Hi,
You need to use synchronous glide ajax here.
https://docs.servicenow.com/bundle/rome-application-development/page/script/ajax/topic/p_AJAX.html
var ga = new GlideAjax('HelloWorld') ; ga.addParam('sysparm_name','helloWorld'); ga.addParam('sysparm_user_name',"Bob"); ga.getXMLWait(); alert(ga.getAnswer());
var HelloWorld = Class.create(); HelloWorld.prototype = Object.extendsObject(AbstractAjaxProcessor, { helloWorld: function() { return "The Server Says Hello " + this.getParameter('sysparm_user_name') + "!"; } } );