How to clear the auto populating values of ListCollector in Catalog
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-26-2024 02:41 PM
Hello,
I would like to push the values based on my requirement using Catalog Client Script and Script Include to catalog but all values are populating based on the column and table I selected while creating the List Collector.
Is there any way to clear the values in List Collector first and based on another field value selection in same catalog should drive what values to populate. Can someone help on this?
Thanks,
Prathibha
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-27-2024 12:11 AM
Hi @Prathibha7 ,
Yes, you can achieve this using a combination of a Catalog Client Script and a Script Include. The idea is to clear the existing values in the List Collector and then dynamically populate it based on the selection in another field.
I've tried to provide you the steps here:
Step 1: Create the Script Include
Create a Script Include to fetch the values based on your requirement.
Navigate to: System Definition > Script Includes.
Create a new Script Include:
Name: DynamicListCollector
API Name: DynamicListCollector
Accessible from: All application scopes
Script:
var DynamicListCollector = Class.create();
DynamicListCollector.prototype = {
initialize: function() {},
getValues: function(dependentValue) {
var result = [];
var gr = new GlideRecord('your_table_name'); // Adjust the table name
gr.addQuery('dependent_field', dependentValue); // Adjust the field name
gr.query();
while (gr.next()) {
result.push(gr.sys_id.toString());
}
return result;
},
type: 'DynamicListCollector'
};
Step 2: Create the Catalog Client Script
Create a Catalog Client Script to dynamically update the List Collector based on the selection of another field.
Navigate to: Service Catalog > Catalog Variables > Catalog Client Scripts.
Create a new Catalog Client Script:
UI Type: All
Type: OnChange
Variable name: The name of the variable that will trigger the change (e.g., dependent_variable)
Script:
function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue === '') {
return;
}
var listCollectorVariable = 'list_collector_variable_name'; // Adjust the List Collector variable name
var ga = new GlideAjax('DynamicListCollector');
ga.addParam('sysparm_name', 'getValues');
ga.addParam('sysparm_dependentValue', newValue);
ga.getXMLAnswer(function(response) {
var values = response.split(',');
var listCollector = g_form.getControl(listCollectorVariable);
var listCollectorName = listCollector.id;
// Clear existing values
g_form.clearOptions(listCollectorName);
// Add new values
for (var i = 0; i < values.length; i++) {
var option = new Option(values[i], values[i]);
listCollector.options.add(option);
}
});
}
Thanks,
Hope this helps.
If my response proves helpful please mark it helpful and accept it as solution to close this thread.