i have 3 variables and based on the check of these variables i need to populate lookup…
- User
i have 3 variables and based on the check of these variables i need to populate lookup variable choices in servicenow catalog items
- Assistant
i have 3 variables and based on the check of these variables i need to populate lookup variable choices in servicenow catalog items
To dynamically populate a Lookup Select Box variable's choices in a ServiceNow catalog item based on the values of three other variables, you have a few options depending on your exact requirements and setup. The most common approach is using a Catalog Client Script (usually an onChange or onLoad client script) combined with a Script Include or AJAX call to fetch the filtered lookup choices.
var1, var2, var3).cmdb_ci or some custom table).var1, var2, var3: Could be single-line text, select box, or any type.lookup_var: This is a Lookup Select Box referencing the appropriate table.Create a Script Include that can accept the parameters and return filtered records in JSON.
Example Script Include (Server-side):
var CatalogLookupHelper = Class.create();
CatalogLookupHelper.prototype = {
initialize: function() {
},
getFilteredChoices: function(var1, var2, var3) {
var results = [];
var gr = new GlideRecord('cmdb_ci'); // or your referenced table
gr.addQuery('field_for_var1', var1); // example filter based on var1
gr.addQuery('field_for_var2', var2); // example filter based on var2
gr.addQuery('field_for_var3', var3); // example filter based on var3
gr.query();
while (gr.next()) {
results.push({
sys_id: gr.sys_id.toString(),
display_value: gr.getDisplayValue()
});
}
return JSON.stringify(results);
},
type: 'CatalogLookupHelper'
};
'cmdb_ci' and field names with your actual table and fields that represent your variables.In your Catalog Item, create an onChange Client Script for each of var1, var2, var3 (or an onLoad script).
Example Catalog Client Script (onChange of var1, var2, var3):
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading) return;
// Get values of all 3 variables regardless of which triggered this change
var var1 = g_form.getValue('var1');
var var2 = g_form.getValue('var2');
var var3 = g_form.getValue('var3');
// Clear current lookup_var choices before refilling
g_form.clearOptions('lookup_var');
// AJAX call to Script Include on server side
var ga = new GlideAjax('CatalogLookupHelper');
ga.addParam('sysparm_name', 'getFilteredChoices');
ga.addParam('var1', var1);
ga.addParam('var2', var2);
ga.addParam('var3', var3);
ga.getXMLAnswer(function(response) {
var choices = JSON.parse(response);
if (choices.length === 0) {
g_form.addOption('lookup_var', '', '-- No Choices Available --');
} else {
// Add empty option at start
g_form.addOption('lookup_var', '', '-- Select --');
choices.forEach(function(choice) {
g_form.addOption('lookup_var', choice.sys_id, choice.display_value);
});
}
});
}
'lookup_var', 'var1', 'var2', 'var3' with your actual variable names.onChange script on all three vars that trigger the lookup update.GlideAjax and Script Includes is the most scalable and clean method.If you want, I can help you build a full example based on your table and variable names. Just provide those details!
Not yet included in a published release.