Catalog Client Script asyncrhonous query to stop submission onSubmit

Katie A
Mega Guru

Hello, I have a Catalog Client Script that performs a GlideRecord query to check if a record already exists with the same name. If a matching record is found, we need to STOP the form from being submitted. We have this working fine on the CMS portal.

However, the new Service Portal does not support synchronous GlideRecord query. So, I can't use gr.query() I need to use a callback such as gr.query(callback).

The issue is that since the callback is asynchronous, it does not actually stop the form from being submitted!

g_form.submitted = false; DOES NOT work. That's because the script proceeds along to submit the form before the callback has a chance to retrieve the value.

How can we stop the submission of a form based on the value returned by an asynchronous callback? We can't use GlideAjax for the same reason, getXMLWait() is no longer supported.

Here is the client script that I am trying to get working in the new Service Portal.

function onSubmit() {

  var group_name = g_form.getValue('u_group_name');

  g_form.hideAllFieldMsgs('error');

  /*Check if group already exists*/

  var rec = new GlideRecord('u_auth_group');

  rec.addQuery('u_group_name', u_group_name);

  rec.query(getAccountResponse);

  }

function getAccountResponse(rec) {

  while (rec.next()) {

  g_form.showFieldMsg('u_group_name', " Group Name exists already, please select another group name",'error');

  g_form.submitted = false; //ISSUE: DOES NOT STOP THE FORM FROM BEING SUBMITTED

  return false;

  }

}

Here is the existing script that works in the CMS portal.

function onSubmit() {

  var group_name = g_form.getValue('u_group_name');

  g_form.hideAllFieldMsgs('error');

  /*Check if group already exists*/

  var rec = new GlideRecord('u_auth_group');

  rec.addQuery('u_group_name', u_group_name);

  rec.query(getAccountResponse);

  while (rec.next()) {

  g_form.showFieldMsg('u_group_name', " Group Name exists already, please select another group name",'error');

  g_form.submitted = false; //Stops the form from being submitted if a result is returned

  return false;

  }

}

1 ACCEPTED SOLUTION

I was able to resolve it by using the callback in the query.




function onSubmit() {


//If ServicePortal


if (!window) {


  if (g_scratchpad.isFormValid) {


              return true;


  }



  g_form.hideAllFieldMsgs('error');


  var actionName = g_form.getActionName();



  //Group Name contain letters numbers and dashes only


  var group_name = g_form.getValue('u_group_name');



  //Check if google group already exists


  var rec = new GlideRecord('u_mc_groups');


  rec.addQuery('u_group', group_name);



  //Callback function to control stop submit asynchronously


  rec.query(function() {


  while (rec.next()) {


  g_form.showFieldMsg('u_group_name', "Group exists already!", 'error');


  return false;


  }


              g_scratchpad.isFormValid = true;


              g_form.submit(actionName);


  });



  return false;


}


}


View solution in original post

12 REPLIES 12

Jace Benson
Mega Sage

make it onchange, require the variable, in your while loop unset the variable.


Interesting solution. I personally wouldn't recommend doing it this way though since you may run into race conditions. I was glad to find a way of doing it onSubmit via the async query.


Kristina2
Tera Contributor

For the same requirement .



I am looking to write a catalog client   script .



When a group name in entered in the request form , if the group is already existing .



It gives us a alert .



I am trying to do it with a AJAX call .



Any help