【问题标题】:ServiceNow Javascript passing variables from GlideAjax functionsServiceNow Javascript 从 GlideAjax 函数传递变量
【发布时间】:2015-08-18 05:26:02
【问题描述】:

我正在使用 ServiceNow。我需要在提交时验证表单。我正在使用带有脚本的 GlideAjax 来验证数据。如何将变量从 Ajax 函数 calendarDate(response) 传递到客户端脚本中的其他函数?当 glide ajax 函数返回错误信息时,我想将变量“isValid”设置为 false。

我使用不涉及 GlideAjax 的客户端脚本很容易做到这一点。我只是将变量 isValid 设置为函数的结果,例如 var isValid = checkLnrDates();

但是,在使用 GlideAjax 时设置一个等于函数调用的变量不会返回任何我可以使用的值。我可能不理解 GlideAjax 函数的调用和处理方式。

目录客户端脚本 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 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;
  }
  }

【问题讨论】:

    标签: javascript ajax validation scope servicenow


    【解决方案1】:

    您需要从 AJAX 往返获取响应才能继续操作,这意味着您实际上并不是异步的。您可能只需调用ajaxCalendarDate.getXMLWait(),然后调用ajaxCalendarDate.getAnswer() 以同步获取响应(请参阅Synchronous GlideAjax

    但是,由于您已经提交,并且您的代码依赖于服务器端函数调用来验证某些输入,您可能只需考虑将此逻辑移动到使用 current.setAbortAction(true) 进行验证和中止的 Before Insert 业务规则中如果您的验证失败。 维基示例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
      }
    }
    

    【讨论】:

    • 好的,感谢您的建议,我已改用 getXMLWait。我已经用 ajaxCalendarDate.getAnswer(); 测试了它。这允许我将该函数中的变量传递给 onSubmit !!!所以我可以根据该输出决定是否提交表单。非常感谢乔伊。
    【解决方案2】:

    试试这个:

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

    【讨论】:

    • ServiceNow 相当于 document.forms[0].submit(); 将是 g_form.submit();
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 2015-07-23
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    • 2017-04-30
    相关资源
    最近更新 更多