- 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 06:21 AM
You already posted the same question and now this is a duplicate question.
You can check for answer already available there and reply back if you have any query.
Regards,
Sumanth
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2022 06:55 AM
Hi

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-26-2022 07:02 AM
If its not working you might have asked there itself.
Okay. Can you share your scripts of what you tried.
Regards,
Sumanth

- 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