2016-07-08 9 views
0

여기서 start_date 및 end_date 날짜에 대해 2 개의 변수를 비교하고 end_date가 start_date보다 큰 경우에만 양식을 제출할 수 있습니다. 그렇지 않으면 양식 제출을 거부하지만 이 코드를 실행하는 동안 무한 루프가 발생하고 getXML (checkDateDiff) 대신 getXMLWait()을 사용하여 비동기로 만들면 모바일 API에서 지원되지 않습니다.ServiceNow : 콜백 함수에서 onSubmit을 호출하면 무한 루프가 발생합니다.

또한 날짜 비교에 도움이되는 클라이언트 스크립트가 많이 있지만 모바일 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 
    } 
} 

답변

0

귀하의 onSubmit() 기능은 항상 mifi 요청에 대한 false을 반환합니다. onSubmit() 함수는 true을 반환하면보다 안전한 제출을 실행할 수 있습니다. 또한 g_form 함수는 서버에서 실행되므로 콜백 함수에서 실행할 수 없습니다.

보다는 당신의 checkDateDiff 함수의 끝에서 g_form.submit()이는 onSubmit() 함수 반환 true 있습니다.

이와 비슷한 기능이 작동합니다. 나는 내가 바꾼 모든 라인에 대해 논평했다 :

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

     // **CHANGED CODE: instead of g_form.submit(), this will return true 
     if(validateTravelEndDate()){ 
      return true; 
     } 
     else{ 
      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')); 

    // **CHANGED CODE: validateTravelEndDate returns the callback value 
    return 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 { 
    // **CHANGED CODE: checkDateDiff will return true 
     return true; 
    } 
} 
+0

안녕하세요. 이것은 내 문제를 해결했습니다. –