Calling onSubmit in a callback function leads to infinite loop

2.2k Views Asked by At

I am comparing 2 variables for dates start_date and end_date and allowing to submit the form only in case end_date is bigger than start_date, else rejecting the form to be submitted, but while running this code, it goes into the infinite loop and if i make this asynchronous by using getXMLWait() instead of getXML(checkDateDiff) it's not supported with mobile APIs.

Also there are lot of client script which help in comparing dates but none of them is supported with mobile APIs.

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

There are 1 best solutions below

0
On BEST ANSWER

Your onSubmit() function always returns false for a mifi request. onSubmit() functions can execute a safer submit when they return a true. Also, g_form functions cannot be run in the callback function, since that is executed on the server.

Rather than have a g_form.submit() at the end of your checkDateDiff function, have onSubmit() function return true.

Something like this should work. I commented every line that I changed:

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