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

tltoulson
Kilo Sage

Hi Leslie,



Try this:



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;


              }


      }


}



Notice how I put the checkLnrDates and the calendarDate functions inside the onSubmit.   This puts those functions under the onSubmit variable scope.   In Javascript, scopes (like functions, if statements, for loops, etc) inherit variables from their parent scopes.   In your original, the scopes looked like this:



- global


        - onSubmit


                  - isValid


        - checkLnrDates


        - calendarDate



So calendarDate, whose only parent scope was global, couldn't find isValid... so it created a new one in its own scope.   By nesting the functions, you get the following scopes:



- global


        - onSubmit


                  - isValid


                  - checkLnrDates


                  - calendarDate



In this scenario, calendarDate inherits the isValid variable from its parent scope, onSubmit.   I hope this helps!



Kind regards,



Travis


Thanks so much Travis.   I tried your modified code by testing with alert(isValid) but that variable is undefined.



Also, I tried nesting the calendarDate(response) function within the checkLnrDates() function (so each function is nested within each other) although isValid is still showing as undefined.



function onSubmit () {


      var isValid = checkLnrDates();



  alert(isValid);


      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;


              }


      }


}


}


It works as expected when I put return false inside of the checkLnrDates() function.



I cant get any return values to pass outside from inside of the glideajax function calendarDate(response).



I also tried isValid = calendarDate(response);


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.