ServiceNow:在回调函数中调用onSubmit会导致无限循环

时间:2016-07-08 18:19:31

标签: datetime servicenow clientscript

这里我比较了日期start_date和end_date的2个变量,并且只有在end_date大于start_date的情况下才允许提交表单,否则拒绝提交的表单,但是在运行此代码时,它会进入无限循环,如果我通过使用getXMLWait()而不是getXML(checkDateDiff)使这个异步,那么移动api不支持它。

此外,还有很多客户端脚本可以帮助比较日期,但移动api不支持这些脚本。

请查看以下代码并提供帮助!!!!

function onSubmit() {
    var requestType = g_form.getValue('request_type');
    if (requestType == 'mifi') {
        console.log("calling validateTravelEndDate()");
        validateTravelEndDate();
        return false;
    } else
        return true;
}

//Helper function which calls a AJAX script include called "ClientDateTimeUtils" which gives the response in a callback where i am deciding whether to submit the form or not based on the status of days result.

function validateTravelEndDate() {
    var startDate = g_form.getValue('travel_start'); //First Date/Time field
    var endDate = g_form.getValue('travel_end'); //Second Date/Time field
    var dttype = 'day'; //this can be day, hour, minute, second. By default it will return seconds.
    console.log("startDate :" + startDate + "endDate :" + endDate);
    var ajax = new GlideAjax('ClientDateTimeUtils'); // This is the script include which can be used for date validation.
    ajax.addParam('sysparm_name', 'getDateTimeDiff');
    ajax.addParam('sysparm_fdt', startDate);
    ajax.addParam('sysparm_sdt', endDate);
    ajax.addParam('sysparm_difftype', dttype);
    console.log("before " + g_form.getValue('travel_end'));
    ajax.getXML(checkDateDiff);
}

// callback function where deciding to go ahead or not with form submission.
function checkDateDiff(response) {
    var answer = response.responseXML.documentElement.getAttribute("answer");
console.log("difference in days:" + answer);
    if (answer <= 0) {
        alert("Travel End date must be after Travel Start date.");
        g_form.setValue('travel_end', '');
        g_form.showFieldMsg('travel_end', 'Please provide a future date', 'error');
       return false;
 } else {
      console.log("%%%%%%%%%%%%%%% Calling g_form.submit()");
      g_form.submit(); // This has some issue as it’s going in the infinite loop and if we just return true/false from here as it’s asynchronous call , it’s not handled by the onSubmit function
  }
}

1 个答案:

答案 0 :(得分:0)

对于[{"name":"prueba0","quantity":"8","price":"574"},{"name":"prueba1","quantity":"8","price":"555"},{"name" :"prueba2","quantity":"7","price":"901"}] 请求,您的onSubmit()函数始终返回false。 onSubmit()函数可以在返回mifi时执行更安全的提交。此外,true函数无法在回调函数中运行,因为它在服务器上执行。

不要在g_form函数的末尾添加g_form.submit(),而是让checkDateDiff函数返回onSubmit()

这样的事情应该有效。我评论了我改变的每一行:

true