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

BALAJI40
Mega Sage

try with this,


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


  })


}



refer 2.1.6 in this link, Mobile Client GlideForm (g form) Scripting - ServiceNow Wiki


it is also quite similar


Thanks, I tried that as well. The showFiledMsg shows up on the form so we know that the callback is definitely working.



I think the issue is that g_form.submitted = false; does not stop the form from being submitted in the context of the callback.



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



var rec = new GlideRecord('u_auth_group');


rec.addQuery('u_group_name', u_group_name);


rec.query(function(rec) {


while (rec.next()) {


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


  g_form.submitted = false; // Form submission is not cancelled


  return false;


  }


  });


Just out of curiosity, what if you return rec.query(function(){});



//other code here


return rec.query(function(){});


//other code here


I tried it and it is the same behavior, g_form.submitted = false; does not stop the form from being submitted in the context of the callback.