
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-30-2016 01:43 PM
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;
}
}
Solved! Go to Solution.
- Labels:
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2016 02:06 PM
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;
}
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-04-2016 09:07 AM
I am having the same issue. Were you able to find a way to make this work?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
11-09-2016 02:06 PM
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;
}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-24-2017 10:33 AM
Hello kt,
Can I know what exactly these two lines are doing.
- g_scratchpad.isFormValid = true;
- 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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-22-2017 03:33 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-20-2017 10:24 AM
Thanks kt617.
This helped me!