- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2025 10:00 PM
I have a list collector variable called asset which refers to the hardware table. My requirment is that whenever any value selected which is of class hardware or server and has install status retired, then a message should be displayed in the form saying Value selected is not correct. How to do this
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2025 10:31 PM
Hi @SHALIKAS , You can create a onChange client script on asset field -
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
//Type appropriate comment here, and begin script below
var assets = g_form.getValue('u_asset');
var ga = new GlideAjax('ScriptIncludeName');
ga.addParam('sysparm_name', 'functionName');
ga.addParam('sysparm_assetIds', assets);
ga.getXML(getInfo);
function getInfo(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != 'false')
g_form.addErrorMessage(answer);
}
}
and then create a client callable script include -
var ScriptIncludeName = Class.create();
ScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
functionName: function() {
var information;
var asset = [];
var assets = this.getParameter('sysparm_assetIds');
var agg = new GlideRecord('cmdb_ci_hardware');
agg.addQuery('sys_id', 'IN', assets);
agg.addEncodedQuery('sys_class_name=cmdb_ci_server^ORsys_class_name=cmdb_ci_hardware^install_status=7');
agg.query();
while (agg.next()) {
asset.push(agg.getValue('name'));
}
if (asset) {
information = 'Following assets are having install status retired.Please correct your selections-\n ' + asset;
return information;
} else
return false;
},
type: 'ScriptIncludeName'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-23-2025 10:31 PM
Hi @SHALIKAS , You can create a onChange client script on asset field -
function onChange(control, oldValue, newValue, isLoading, isTemplate) {
//Type appropriate comment here, and begin script below
var assets = g_form.getValue('u_asset');
var ga = new GlideAjax('ScriptIncludeName');
ga.addParam('sysparm_name', 'functionName');
ga.addParam('sysparm_assetIds', assets);
ga.getXML(getInfo);
function getInfo(response) {
var answer = response.responseXML.documentElement.getAttribute("answer");
if (answer != 'false')
g_form.addErrorMessage(answer);
}
}
and then create a client callable script include -
var ScriptIncludeName = Class.create();
ScriptIncludeName.prototype = Object.extendsObject(AbstractAjaxProcessor, {
functionName: function() {
var information;
var asset = [];
var assets = this.getParameter('sysparm_assetIds');
var agg = new GlideRecord('cmdb_ci_hardware');
agg.addQuery('sys_id', 'IN', assets);
agg.addEncodedQuery('sys_class_name=cmdb_ci_server^ORsys_class_name=cmdb_ci_hardware^install_status=7');
agg.query();
while (agg.next()) {
asset.push(agg.getValue('name'));
}
if (asset) {
information = 'Following assets are having install status retired.Please correct your selections-\n ' + asset;
return information;
} else
return false;
},
type: 'ScriptIncludeName'
});
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-24-2025 05:40 AM
Here’s how you can implement this:
Steps to Implement the Validation
1. Identify the List Collector Variable
Ensure you have a list collector variable (e.g., asset) that refers to the alm_hardware table.
2. Create a Client Script
Use a Client Script to validate the selected assets when the form is submitted or when the selection changes.
3. Write the Client Script
The script will:
Get the selected values from the list collector.
Query the alm_hardware table to check the class and install status of each selected asset.
Display a message if any selected asset is invalid.
Example Client Script
Here’s an example of a Client Script to achieve this:
function onChange(control, oldValue, newValue, isLoading, isTemplate) { if (isLoading || newValue === '') { return; } // Get the list collector variable var assetListCollector = g_form.getControl('asset'); // Replace 'asset' with the actual variable name // Get the selected assets var selectedAssets = g_form.getListCollectorValues('asset'); // Replace 'asset' with the actual variable name // Flag to track if any invalid asset is selected var invalidAssetFound = false; // Loop through the selected assets selectedAssets.forEach(function(assetSysID) { // Query the hardware table var hardwareGR = new GlideRecord('alm_hardware'); if (hardwareGR.get(assetSysID)) { // Check if the asset is of class 'hardware' or 'server' and has install status 'retired' if ((hardwareGR.getValue('sys_class_name') == 'hardware' || hardwareGR.getValue('sys_class_name') == 'server') && hardwareGR.getValue('install_status') == 'retired') { invalidAssetFound = true; } } }); // Display a message if an invalid asset is found if (invalidAssetFound) { g_form.addErrorMessage('Value selected is not correct. Please select assets that are not retired.'); } else { g_form.clearMessages(); } }