ServiceNow Javascript 从 GlideAjax 函数传递变量
ServiceNow Javascript passing variables from GlideAjax functions
我正在使用 ServiceNow。我需要在提交时验证表单。我正在使用带有脚本的 GlideAjax 来验证数据。如何将 Ajax 函数 calendarDate(response) 中的变量传递给客户端脚本中的其他函数?当glide ajax函数return出现错误信息时,我想将变量"isValid"设置为false。
我使用不涉及 Glide 的客户端脚本轻松完成了此操作Ajax。我只是将变量 isValid 设置为函数的结果,例如 var isValid = checkLnrDates();
但是,在使用 GlideAjax 时设置一个等于函数调用的变量并没有 return 我可以使用的任何值。我可能不理解 GlideAjax 函数的调用和处理方式。
提交时目录客户端脚本
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 the input data
var ajaxCalendarDate = new GlideAjax('ClientDateTimeUtils');
ajaxCalendarDate.addParam('sysparm_name', 'validateCalendarDate');
ajaxCalendarDate.addParam('sysparm_userDate', start);
ajaxCalendarDate.getXML(calendarDate);
}
function calendarDate(response){
//This is where we get the response returned from the ClientDateTimeUtils script include ajax function
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != 'true'){
g_form.showFieldMsg('start_date', answer,'error');
//How can I pass the value of a variable to the function above? I want to set isValid to false
isValid = false;
return false;
}
}
试试这个:
function onSubmit(){
checkLnrDates();
return false;
}
function checkLnrDates() {
var start = g_form.getValue('start_date');
//Check calendar date format valid YYYY-MM-DD
//Script include ClientDateTimeUtils checks the input data
var ajaxCalendarDate = new GlideAjax('ClientDateTimeUtils');
ajaxCalendarDate.addParam('sysparm_name', 'validateCalendarDate');
ajaxCalendarDate.addParam('sysparm_userDate', start);
ajaxCalendarDate.getXML(calendarDate);
}
function calendarDate(response){
//This is where we get the response returned from the ClientDateTimeUtils script include ajax function
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != 'true'){
g_form.showFieldMsg('start_date', answer,'error');
return false;
}
else
g_form.submit();
}
您需要从 AJAX 往返行程中获得响应才能继续,这一事实意味着您实际上并不是异步的。您可能只调用 ajaxCalendarDate.getXMLWait()
然后调用 ajaxCalendarDate.getAnswer()
以同步获取响应(参见 Synchronous GlideAjax)
但是,由于您已经提交,并且您的代码依赖于服务器端函数调用来验证某些输入,您可能只考虑将此逻辑移至插入前业务规则中,该规则使用 current.setAbortAction(true)
如果您的验证失败。
Wiki 示例 here.
您的业务规则类似于:
function onBefore(current, previous) {
if (!CliendDateTimeUtils.validateCalendarDate(current.start_date)) {
current.setAbortAction(true); // Don't save the record
gs.addErrorMessage("Start date is not valid"); // Add an error message for the user
}
}
我正在使用 ServiceNow。我需要在提交时验证表单。我正在使用带有脚本的 GlideAjax 来验证数据。如何将 Ajax 函数 calendarDate(response) 中的变量传递给客户端脚本中的其他函数?当glide ajax函数return出现错误信息时,我想将变量"isValid"设置为false。
我使用不涉及 Glide 的客户端脚本轻松完成了此操作Ajax。我只是将变量 isValid 设置为函数的结果,例如 var isValid = checkLnrDates();
但是,在使用 GlideAjax 时设置一个等于函数调用的变量并没有 return 我可以使用的任何值。我可能不理解 GlideAjax 函数的调用和处理方式。
提交时目录客户端脚本
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 the input data
var ajaxCalendarDate = new GlideAjax('ClientDateTimeUtils');
ajaxCalendarDate.addParam('sysparm_name', 'validateCalendarDate');
ajaxCalendarDate.addParam('sysparm_userDate', start);
ajaxCalendarDate.getXML(calendarDate);
}
function calendarDate(response){
//This is where we get the response returned from the ClientDateTimeUtils script include ajax function
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != 'true'){
g_form.showFieldMsg('start_date', answer,'error');
//How can I pass the value of a variable to the function above? I want to set isValid to false
isValid = false;
return false;
}
}
试试这个:
function onSubmit(){
checkLnrDates();
return false;
}
function checkLnrDates() {
var start = g_form.getValue('start_date');
//Check calendar date format valid YYYY-MM-DD
//Script include ClientDateTimeUtils checks the input data
var ajaxCalendarDate = new GlideAjax('ClientDateTimeUtils');
ajaxCalendarDate.addParam('sysparm_name', 'validateCalendarDate');
ajaxCalendarDate.addParam('sysparm_userDate', start);
ajaxCalendarDate.getXML(calendarDate);
}
function calendarDate(response){
//This is where we get the response returned from the ClientDateTimeUtils script include ajax function
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != 'true'){
g_form.showFieldMsg('start_date', answer,'error');
return false;
}
else
g_form.submit();
}
您需要从 AJAX 往返行程中获得响应才能继续,这一事实意味着您实际上并不是异步的。您可能只调用 ajaxCalendarDate.getXMLWait()
然后调用 ajaxCalendarDate.getAnswer()
以同步获取响应(参见 Synchronous GlideAjax)
但是,由于您已经提交,并且您的代码依赖于服务器端函数调用来验证某些输入,您可能只考虑将此逻辑移至插入前业务规则中,该规则使用 current.setAbortAction(true)
如果您的验证失败。
Wiki 示例 here.
您的业务规则类似于:
function onBefore(current, previous) {
if (!CliendDateTimeUtils.validateCalendarDate(current.start_date)) {
current.setAbortAction(true); // Don't save the record
gs.addErrorMessage("Start date is not valid"); // Add an error message for the user
}
}