
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2016 11:56 AM
Hi,
I have a form where it's critical that the user submits the correct data the first time. I need to make the following onSubmit client script prompt the user to confirm and abort if they cancel. I know I need a return false; statement somewhere to abort the action, but I can't figure out where to put it or the confirm statement var doIt = confirm('You are about to enter ' + answer + ' hours of ' + timeType + ' time against this task. Are you sure?');
function onSubmit() {
//Type appropriate comment here, and begin script below
var cdt = g_form.getValue('u_start_time'); //First Date/Time field
var sdt = g_form.getValue('u_end_time'); //Second Date/Time field
var dttype = 'hour'; //this can be day, hour, minute, second. By default it will return seconds.
var timeType = g_form.getValue('u_time_allocation');
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_sdt', sdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
}
}
TIA!
Karla
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2016 12:24 PM
It's because we're telling it to process in the background, (async).
Change it to a synchronous GlideAjax call and it will work. I just confirmed it.
function onSubmit() {
//Type appropriate comment here, and begin script below
var cdt = g_form.getValue('u_start_time'); //First Date/Time field
var sdt = g_form.getValue('u_end_time'); //Second Date/Time field
var dttype = 'hour'; //this can be day, hour, minute, second. By default it will return seconds.
var timeType = g_form.getValue('u_time_allocation');
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_sdt', sdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXMLWait();
var answer = ajax.getAnswer();
if (! confirm('You are about to blah blah blah'))
return false;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2016 12:01 PM
Hi Carla,
It looks like you are using GlideAjax to get the difference between two dates - THANK YOU FOR USING GLIDEAJAX!
That being said, are you looking to do the confirm after you get the result back? In that case, your callback function would be:
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
alert(answer);
if (!confirm('You are about to enter blah blah blah. Are you sure?'))
return false;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2016 12:13 PM
The record still gets created.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2016 12:16 PM
can you post the code here

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2016 12:22 PM
I believe I put everything exactly where you suggested, and when testing I am clicking the cancel button.
function onSubmit() {
//Type appropriate comment here, and begin script below
var cdt = g_form.getValue('u_start_time'); //First Date/Time field
var sdt = g_form.getValue('u_end_time'); //Second Date/Time field
var dttype = 'hour'; //this can be day, hour, minute, second. By default it will return seconds.
var timeType = g_form.getValue('u_time_allocation');
var ajax = new GlideAjax('ClientDateTimeUtils');
ajax.addParam('sysparm_name','getDateTimeDiff');
ajax.addParam('sysparm_fdt', cdt);
ajax.addParam('sysparm_sdt', sdt);
ajax.addParam('sysparm_difftype', dttype);
ajax.getXML(doSomething);
function doSomething(response){
var answer = response.responseXML.documentElement.getAttribute("answer");
var doIt = confirm('You are about to enter ' + answer + ' hours of ' + timeType + ' time against this task. Are you sure?');
if(!doit){
return false;
}
}
}