Request Submits even though OnChange Client Script is still executing
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2024 06:55 AM
I have a request that allows end users to request access to a VNC machine. We have a string field on the request where the end user types the hostname of the machine. Then, an onChange client script runs and verifies whether it is a valid hostname by checking if the value is a name (u_name) of a record on the u_vnc_machine table. If it is not a valid hostname, it will clear the field and display a message saying that it is not a valid name. This works unless the user fills out all of the other mandatory fields on the form, then enters some text into the hostname field, and just clicks submit. When they click submit, you can see that the field gets cleared of its value because it is not a valid hostname, and it displays the error message, but it still lets the request be submitted. Below is the code
Here is the onChange Client Script;
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var machineName = g_form.getValue('machine_name');
var ga = new GlideAjax('global.checkMachineHostname');
ga.addParam('sysparm_name','checkUserInput');
ga.addParam('sysparm_machineName',machineName);
ga.getXML(OutputParse);
function OutputParse(response) {
var status = response.responseXML.documentElement.getAttribute("answer");
if(status != 'true'){
g_form.clearValue('machine_name');
g_form.showFieldMsg('machine_name',"The entered machine name is either invalid or not configured for VNC. Please try again.",'error');
}
}
}
Here is the Script Include:
var checkMachineHostname = Class.create();
checkMachineHostname.prototype = Object.extendsObject(global.AbstractAjaxProcessor, {
checkUserInput : function ()
{
var machineName = this.getParameter('sysparm_machineName');
//gs.info("machine name "+machineName);
var gr = new GlideRecord('u_vnc_machines');
gr.addQuery('u_name',machineName);
gr.query();
if(gr.next()){
return true;
}
else{
return false;
}
},
type: 'checkMachineHostname'
});
Here is what the request looks like:
In this example I typed in "Testing" which is not a valid vnc machine on the u_vnc_machine table. It will correctly clear the value if you click off the field but if you enter a value and click submit right after it will let the user submit the request. Even though it shows the value emptied on the SP, the RITM still has whatever value they entered still there. It is almost as if the onChange Script doesn't get finished running.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2024 07:11 AM
unfortunately, this cannot work and you need a completely different approach. Please check the following article: https://www.servicenow.com/community/developer-blog/how-to-async-glideajax-in-an-onsubmit-script/ba-...
Maik

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-19-2024 07:17 AM
Hi,
This is part of User training and user should wait until validation is complete.
Alternatively you can perform same check in onSubmit client script.
Create onSubmit Client script and make GlideAjax call (synchronous ) and return false if hostname is not valid.
If you don't want to use additional glideAjax call then you can create one variable of type checkbox (name it "Is Valid Hostname") set it true or false based on GlideAjax result. (you can keep it hidden on form using UI Policy)
In your onSubmit client script just use simple logic:
return g_form.getValue('is_valid_host_name') == true;
Thanks
Anil Lande