Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

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

Chuck Tomasi
Tera Patron

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;


}    


The record still gets created.


can you post the code here


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;


}


}      


}



Client Script.jpg