- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2022 05:35 AM
Hi All,
I am having a requirement on catalog task form. On the form there is two editable field hostname and serial number. Serial Number auto populates when hostname is filled but if the hostname is of a new asset which is not present in pc_hardware table then the user needs to manually enter the serial number.
In manual case, we need to validate if the serial number is already present in pc_hardware table or not. If yes, we need to show the error msg with the name of asset to which the serial number is assigned and prevent saving the form.
As I am new and learning, kindly help me with the script to validate serial number and display error message.
Solved! Go to Solution.
- Labels:
-
Service Catalog

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2022 05:28 AM
Can you try below scripts :
Yup! You can update scripts as below
Client script :
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
var ga = new GlideAjax('global.CMDBClientUtils'); //update script include name as needed
ga.addParam('sysparm_name', 'checkForSerialNumber');
ga.addParam('sysparm_serialnumber', g_form.getValue("serial_number"));
ga.addParam('sysparm_hostname', g_form.getValue('host_name')); //check the backend value of hostname variable
ga.getXML(checkSerialNumber);
}
function checkSerialNumber(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != 'no') {
g_form.clearValue('serial_number');
g_form.addErrorMessage(answer);
}
}
Script Include
var CMDBClientUtils = Class.create();
CMDBClientUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkForSerialNumber: function () {
try {
var answer;
var serialNumber = this.getParameter('sysparm_serialnumber');
var hostName = this.getParameter('sysparm_hostname');
var grHardware = new GlideRecord('cmdb_ci_pc_hardware');
grHardware.addQuery('serial_number', serialNumber);
grHardware.addQuery('name', '!=', hostName);
grHardware.query();
if (grHardware.next()) {
answer = 'Serial Number already exists, found in asset: ' + grHardware.getValue('name');
}
else {
answer = 'no';
}
return answer;
} catch (ex) {
gs.error(ex);
}
},
type: 'CMDBClientUtils'
});
If the above is not working, just share the screenshot of your form which shows the hostname field and serial number field.
Mark as correct and helpful if it solved your query.
Regards,
Sumanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2022 08:18 AM
To do that, we need to shake things up a little. Instead of returning a True / False statement from our Script Include function, we should return the error or info message we wish to show instead.
Script Include
var CMDBClientUtils = Class.create();
CMDBClientUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
checkForSerialNumber: function () {
try {
var serialNumber = this.getParameter('sysparm_serialnumber');
var grHardware = new GlideRecord('cmdb_ci_pc_hardware');
grHardware.addQuery('serial_number', serialNumber);
grHardware.setLimit(1);
grHardware.query();
// We are just going to return a simple error message if we do / dont find the
if (grHardware.next()) {
return 'Serial Number already exists, found in asset: ' + grHardware.getValue('name');
}
} catch (ex) {
gs.error(ex);
}
},
type: 'CMDBClientUtils'
});
Client Script
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}
// GlideAjax, if you're not familiar with the concepts I suggest spending some time looking it up, its super useful.
var ga = new GlideAjax('global.CMDBClientUtils');
ga.addParam('sysparm_name', 'checkForSerialNumber');
ga.addParam('sysparm_serialnumber', g_form.getValue("serial_number"));
ga.getXML(checkSerialNumber);
}
function checkSerialNumber(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer) {
g_form.addErrorMessage(answer);
g_form.clearValue('serial_number');
}
}
The modifications to the code mean that when we DO find Serial Number, we return the Error message we want to the Client Script, but the Error message is formulated in the Script Include (Server Side) rather than the Client Script (Client Side). There is a reason we do this, but I won't cover it here.
The error message I have put in the script include is just an example, you will need to modify it to show and say exactly what you want it to.
Please mark the answer as correct if this has solved your issue, or upvote the answer if it was helpful.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2022 10:20 PM
Doing so will clear the serial number value even if it is auto populated from the hostname.
In her question, she mentioned that serial number will be auto populated after hostname is filled.
So with the above script, the auto populated value gets cleared because it is already present in table.
🙂
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2022 01:08 AM
Hi

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2022 02:30 AM
So before I give you new script, answer this:
Is the hostname field a reference field or string field?
If reference field then it is referring to which table?
Regards,
Sumanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-27-2022 02:43 AM
There could be 3 scenerios:
1. Hostname is in table and serial number autopopulates, which is existing functionality.
> It should save the form without error.
2. Hostname is not in table and serial number match with table.
> It should display error msg with hostname of that asset and not allow user to save the form.
3. Hostname is not in table and serial number is not in table.
> It should save form without error.