Catalog Item Single Line Text Validation

Konner Lester
Tera Expert

We have a catalog item that allows end users to request VNC access to different hosted machines. Currently, this request involves a string field that permits users to enter any value. However, we aim to modify this field to only accept valid hostnames.

 

We maintain a table that has various VNC machines (u_vnc_machines). This table would be used to check if the machine exists or not. 

 

While a simpler solution would be to use a dropdown variable referencing this table, we want to avoid users selecting a machine arbitrarily and thinking, "Oh, I need that access." Instead, we want them to input the actual machine name. If a user enters a hostname that doesn't exist in the u_vnc_machines table, the entered value should be cleared, and a message like "This is not a valid machine" should be displayed, prompting them to try a different name.

 

I believe the best way to implement this is with an onChange Client Script that examines the string variable. The script can then call a Script Include to look up the current value of the string and verify if that name exists in the u_vnc_machine table. Please let me know if you know how I could go about creating this string field validation. Thank you

1 ACCEPTED SOLUTION

Amit Verma
Kilo Patron
Kilo Patron

Hi @Konner Lester 

 

You are correct with your approach. It do requires a combination of an On-Change Catalog Client Script and a Client Callable Script Include which could do the Glide query in the u_vnc_machine table and check if the input machine hostname entry exists or not in the table and send back the response to the client script through which we can either retain the variable value in case of correct hostname or we can clear the field value and display field error message "Invalid Machine Name entered". 

 

Below is the client callable script include for your reference. Please modify accordingly.

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_machinename',machineName);
		gr.query();
		if(gr.next()){
			return true;
		}
		else{
			return false;
		}
	},

    type: 'checkMachineHostname'
});

 

AmitVerma_0-1705067694471.png

On-Change Catalog Client Script :

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	var machineName = g_form.getValue('machineName');
	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('machineName');
				g_form.showFieldMsg('machineName','Invalid Machine Name entered!!!','error');
			}			
	}
	
}

AmitVerma_1-1705067742474.png

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

View solution in original post

5 REPLIES 5

Brad Bowman
Kilo Patron
Kilo Patron

Your approach with an onChange Catalog Client Script that calls a Script Include via GlideAjax is the recommended approach.  Here is an excellent guide, if you are unfamiliar

https://www.servicenow.com/community/developer-articles/glideajax-example-cheat-sheet-updated/ta-p/2... 

Give the scripts a shot, then let me know if you get stuck.

Amit Verma
Kilo Patron
Kilo Patron

Hi @Konner Lester 

 

You are correct with your approach. It do requires a combination of an On-Change Catalog Client Script and a Client Callable Script Include which could do the Glide query in the u_vnc_machine table and check if the input machine hostname entry exists or not in the table and send back the response to the client script through which we can either retain the variable value in case of correct hostname or we can clear the field value and display field error message "Invalid Machine Name entered". 

 

Below is the client callable script include for your reference. Please modify accordingly.

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_machinename',machineName);
		gr.query();
		if(gr.next()){
			return true;
		}
		else{
			return false;
		}
	},

    type: 'checkMachineHostname'
});

 

AmitVerma_0-1705067694471.png

On-Change Catalog Client Script :

function onChange(control, oldValue, newValue, isLoading) {
   if (isLoading || newValue == '') {
      return;
   }
	var machineName = g_form.getValue('machineName');
	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('machineName');
				g_form.showFieldMsg('machineName','Invalid Machine Name entered!!!','error');
			}			
	}
	
}

AmitVerma_1-1705067742474.png

 

Thanks & Regards

Amit Verma


Please mark this response as correct and helpful if it assisted you with your question.

This works perfectly! Thank you for the help.

Going back on this I found an issue where if you enter text into the field and click submit the request will clear the value of the string field but it will still submit the request even though the value is not a VNC machine. 

Here is the onChange Client Script;

KonnerLester_0-1705675183515.png

 

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:

KonnerLester_1-1705675253487.png

 

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. 

KonnerLester_2-1705675396868.png