GlideAjax passing variables between mutliple functions to another

leslie8
Mega Expert

I need to validate a form onSubmit. I am using GlideAjax with a script include to validate data. How can I pass variables from the Ajax function to other functions within the Client Script? When the glide ajax function returns an error message, I want to set the variable "isValid" to false.

I have done this easily with Client Scripts that do not involve GlideAjax. I simply set the variable isValid to the result of the function such as var isValid = checkLnrDates();

However, setting a variable equal to the function call does not return any value that I can use.

Catalog Client Script 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;

  }

  }

1 ACCEPTED SOLUTION

Hi Leslie,



Sorry, I failed to read through it entirely.   Jim has got it right.   Since you are doing on Submit, you will want to block the function from continuing by using getXMLWait.



function onSubmit () {


      var isValid = calendarDate(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.getXMLWait();


              return ajaxCalendarDate.getAnswer();


      }



      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


                      return false;


              }


              return true;


      }


}



I've done a little more tweaking on this one to make it synchronous using getXMLWait.   I haven't tested it yet, but the principle should be on the right path.


View solution in original post

7 REPLIES 7

Travis you are wonderful. Thank you SOOOO much!!!



The line return ajaxCalendarDate.getAnswer(); is allowing me to now pass variables from that function to the onSubmit so I can decide whether to submit the form or not based on that output. Thanks so much to both of you.


Jim Coyne
Kilo Patron

You are going to want to use "getXMLWait" in your Ajax call because the response will probably come back after the client script finishes running and you've moved on from the form.   The onSubmit Client Script is one of a very few scenarios where you do not want to run an asynchronous Ajax call.



The "getXMLWait" will wait for the response before continuing.     It might be a little counter-intuitive, but you are validating data and the user is not really waiting to do anything else, just for the submit to finish.


AH ok, good to know. Do you think that might be why I can't get variables outside of that function?