Catalog Client Script: Form submission blocked after successful asynchronous GlideAjax validation
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
3 hours ago - last edited 2 hours ago
Hi guys,
I'm trying to implement a duplicate validation for a Catalog Item in ServiceNow, but I'm running into an issue with asynchronous GlideAjax validation in an onSubmit Catalog Client Script.
Background
We have a Catalog Item used to create functional mailboxes. The form is presented in our frontend (service portal) and collects several values from the user.
These values are automatically combined into a variable called final_address, which generates an identifier in the following format:
sdk:test.gron:0203:example.se
When the Catalog Item is submitted, this value is eventually stored in the u_identifier field of our custom table u_cmdb_ci_sdk.
Before allowing the user to submit the form, I want to check whether the generated final_address already exists as a u_identifier in the u_cmdb_ci_sdk table.
If the identifier already exists, the submission should be blocked and an error message should be displayed.
If the identifier does not exist, the form should be submitted normally.
I've created a client-callable Script Include (VGRsdkUtilAjax) extending AbstractAjaxProcessor, with the following function:
checkIdentifier: function() {
var identifier = String(this.getParameter('sysparm_identifier') || '').trim();
if (gs.nil(identifier))
return 'false';
var gr = new GlideRecord('u_cmdb_ci_sdk');
gr.addQuery('u_identifier', identifier);
gr.setLimit(1);
gr.query();
return gr.hasNext() ? 'true' : 'false';
},I've also added an onSubmit Catalog Client Script:
var checkedIdentifier = '';
var identifierCheckInProgress = false;
function onSubmit() {
var identifier = g_form.getValue('final_address');
if (checkedIdentifier === identifier) {
return true;
}
if (identifierCheckInProgress) {
return false;
}
identifierCheckInProgress = true;
var ga = new GlideAjax('VGRsdkUtilAjax');
ga.addParam('sysparm_name', 'checkIdentifier');
ga.addParam('sysparm_identifier', identifier);
ga.getXMLAnswer(function(answer) {
identifierCheckInProgress = false;
if (answer === 'true') {
g_form.showFieldMsg(
'final_address',
'This SDK functional mailbox already exists. Please choose another name.',
'error'
);
return;
}
checkedIdentifier = identifier;
g_form.submit();
});
return false;
}
The validation itself works as expected when the identifier already exists. The user receives the error message and the form submission is blocked.
However, when I change the input values so that final_address generates a unique identifier, the error message disappears, but the form still does not submit.
My understanding is that g_form.submit() should trigger the submission again, and that the checkedIdentifier variable should allow the second onSubmit execution to proceed.
However, this does not seem to work as expected.
Is this the recommended approach for asynchronous server-side validation in an onSubmit Catalog Client Script?
I would appreciate any suggestions or examples of a recommended implementation.
Thanks in advance!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
I think you're over-complicating this, and not using the inherent capabilities. You can simply do the check then return true or false:
function onSubmit() {
var identifier = g_form.getValue('final_address');
var ga = new GlideAjax('VGRsdkUtilAjax');
ga.addParam('sysparm_name', 'checkIdentifier');
ga.addParam('sysparm_identifier', identifier);
ga.getXMLAnswer(function(answer) {
if (answer === 'true') {
g_form.showFieldMsg(
'final_address',
'This SDK functional mailbox already exists. Please choose another name.',
'error'
);
return false;
}
return true;
});
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
Hey @Brad Bowman , the thing is, then it goes and it submits, even though there is a duplicate value in our custom table.
So the error message fires, which is correct, but the request gets submitted anyways.
Maybe there has to do with the existence of another onSubmit script for the same catalog item? Or does that not matter?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
2 hours ago
That can't be helping. You should only have/need one onSubmit to prevent confusion and conflicts. Use getXMLWait instead of getXMLAnswer to ensure the submission waits for the answer from the server - unless you're using this with Service Portal or Employee Center, then you'll have to use one of the workarounds that there are many posts on.
