
- 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:25 PM
For testing purposes, use synchronous ajax like this and see if it works
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);
ga.getXMLWait();
var answer=ga.getAnswer();
var doIt = confirm('You are about to enter ' + answer + ' hours of ' + timeType + ' time against this task. Are you sure?');
if(!doit){
return false;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2016 12:26 PM
He he. We were working on the same answer at the same time.
FWIW, I tested synchronous, it worked.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2016 12:03 PM
Karla,
You can add this in the call Back function
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;
}
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-19-2016 12:16 PM
The record still gets created with this method as well.
Is it possibly because of the way the doSomething function is called?

- 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;
}