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

Rajdeep Ganguly
Mega Guru

Sure, you can achieve this by creating an onChange client script and a script include. Here's a step-by-step guide:

1. **Create an onChange Client Script:**
- Navigate to System Definition > Client Scripts.
- Click on New to create a new client script.
- Set the Type to "onChange".
- Set the Table to your catalog item table.
- Set the Field to the string field that needs validation.
- In the script section, write a script to call a GlideAjax function that will call the script include.

Here's a sample code for the client script:

javascript
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('CheckVNCMachine');
ga.addParam('sysparm_name', 'validateMachine');
ga.addParam('sysparm_machine_name', newValue);
ga.getXMLAnswer(checkMachineExists);
}

function checkMachineExists(answer) {
if (answer == 'false') {
g_form.clearValue('your_field_name');
g_form.showFieldMsg('your_field_name', 'This is not a valid machine', 'error');
}
}


2. **Create a Script Include:**
- Navigate to System Definition > Script Includes.
- Click on New to create a new script include.
- Set the Name to "CheckVNCMachine".
- Set the Accessible from to "This application scope only".
- In the script section, write a script to check if the machine exists in the u_vnc_machines table.

Here's a sample code for the script include:

javascript
var CheckVNCMachine = Class.create();
CheckVNCMachine.prototype = Object.extendsObject(AbstractAjaxProcessor, {
validateMachine: function() {
var machineName = this.getParameter('sysparm_machine_name');
var machineGR = new GlideRecord('u_vnc_machines');
machineGR.addQuery('u_machine_name', machineName);
machineGR.query();
if (machineGR.next()) {
return 'true';
} else {
return 'false';
}
},
type: 'CheckVNCMachine'
});


Please replace 'your_field_name' and 'u_machine_name' with your actual field names. Also, make sure the user has the necessary roles to access the u_vnc_machines table.


nowKB.com