ajax - ServiceNow Javascript passing variables from GlideAjax functions -
i using servicenow. need validate form onsubmit. using glideajax script include validate data. how can pass variables ajax function calendardate(response) other functions within client script? when glide ajax function returns error message, want set variable "isvalid" false.
i have done client scripts not involve glideajax. set variable isvalid result of function such var isvalid = checklnrdates();
however, setting variable equal function call when using glideajax not return value can use. perhaps not understanding way glideajax functions called , handled.
catalog client script onsubmit
function onsubmit () { var isvalid = checklnrdates(); if (isvalid == false) { g_form.submitted = false; return false; } } function checklnrdates() { var start = g_form.getvalue('start_date'); //check calendar date format valid yyyy-mm-dd //script include clientdatetimeutils checks input data var ajaxcalendardate = new glideajax('clientdatetimeutils'); ajaxcalendardate.addparam('sysparm_name', 'validatecalendardate'); ajaxcalendardate.addparam('sysparm_userdate', start); ajaxcalendardate.getxml(calendardate); } function calendardate(response){ //this response returned clientdatetimeutils script include ajax function var answer = response.responsexml.documentelement.getattribute("answer"); if (answer != 'true'){ g_form.showfieldmsg('start_date', answer,'error'); //how can pass value of variable function above? want set isvalid false isvalid = false; return false; } }
the fact need response ajax round-trip before can proceed means you're not asynchronous. call ajaxcalendardate.getxmlwait() , call ajaxcalendardate.getanswer() response synchronously (see synchronous glideajax)
however, since you're submitting, , code relies on server-side function call validate input, might consider moving logic before insert business rule validates, , aborts using current.setabortaction(true) if validation fails. wiki example here.
your business rule like:
function onbefore(current, previous) { if (!clienddatetimeutils.validatecalendardate(current.start_date)) { current.setabortaction(true); // don't save record gs.adderrormessage("start date not valid"); // add error message user } }
Comments
Post a Comment