Client script abort action

kchorny
Tera Guru

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

1 ACCEPTED SOLUTION

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;


}


View solution in original post

16 REPLIES 16

Hi Abhinay/Chuck,



One question - can I use g_scratchpad for 'onsubmit' client scripts ? I've written SYNC GlideAjax scripts for these type of onsubmit validations, so was wondering if the same could be accomplished via a g_scratchpad script ?



Cheers


Hi Suhas,



g_scratchpad can be used to push information from the server to the client script. You set the contents up in a Display business rule. Often times this is used in an onLoad client script, but there shouldn't be any reason why it cannot be used in an onSubmit as well. Just remember that it's a 'one time' push of information when the form is loaded and does not refresh the way a GlideAjax call would do with an onChange client script.



http://wiki.servicenow.com/index.php?title=Client_Script_Best_Practices