Dynamic Reference Field Values Based on Select box Choices in Catalog Item
In the catalog item, There are 2 fields:
1. 'Environment' of type select box with choices(Dev and Prod)
2. 'List of CIs' which is of reference type referencing to [cmdb_ci] table
On the selection of Environment as Dev, the 'List of CIs' will be showing all the CIs of environment Development and similarly if Environment is selected as Prod, the List of CIs will show the list of environment Production.
Implementation Steps:
We will utilize the advance reference qualifier of the 'List of CIs' field in this scenario and call the script include and pass the service portal selected Environment value as parameter.
Script Include:
Client callable - true
var CustomQueryUtils = Class.create();
CustomQueryUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
showEnvCIs: function(getEnvVal) {
var listOfCIs = '';
var grCI = new GlideRecord('cmdb_ci');
grCI.addQuery('environment', getEnvVal);
grCI.query();
while (grCI.next()) {
listOfCIs = 'environment=' + getEnvVal;
return listOfCIs;
}
},
type: 'CustomQueryUtils'
});
In the variable 'List of CIs', we will define in the advance reference qualifier as below:
javascript: new CustomQueryUtils().showEnvCIs(current.variables.environment);
The result will now show the list of CIs based on the Environment selected. If Prod, then only environment type Production will be visible in the list of CIs.
If we want to pass multiple values as parameter, that could be achieved as below:
Advanced reference qualifier of 'List of CIs' :
javascript: new CustomQueryUtils().showEnvCIs(current.variables.environment, current.variables.division);
Client callable Script Include:
var CustomQueryUtils = Class.create();
CustomQueryUtils.prototype = Object.extendsObject(AbstractAjaxProcessor, {
showEnvCIs: function(getEnvVal, getDivisionVal) {
//Logic goes here
},
type: 'CustomQueryUtils'
});
Here,
'getEnvVal' will have the value selected in the variable Environment
'getDivisionVal' will have the value selected in the variable Division
If this helped you in any way, Please hit Helpful. Thanks.
https://www.servicenow.com/community/itsm-articles/dynamic-reference-field-values-based-on-select-box-choices-in/ta-p/2957045