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

I am having the same issue. Were you able to find a way to make this work?


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;


}


}


Hello kt,




Can I know what exactly these two lines are doing.


  1. g_scratchpad.isFormValid = true;  
  2. g_form.submit(actionName);

Because i have used same code with out using g_scratchpad.isFormValid and g_form.submit(actionName), how these two lines are stoping the asynchronous function.I got confused can you brief me..?



Thanks,


Nithin.


@kt617



Great work!!!!   I spent almost two days in finding solution to prevent submission on-submit for certain conditions.


Just few modifications i did on my part and your script worked like a charm.


Thanks kt617.


This helped me!